没有使用lodash sortBy()获取排序数据

我有一些对象,我想按名称排序 . 我正在使用lodash库并使用_.sortBy() . 但通过使用此功能,我没有得到适当的响应 .

以下是我的activeUser数组

[ { date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
    time: '10:30',
    user_name: 'Happy Teachers DAy',
    profilepic: 'http://192.168.0.109:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg',
    activeType: 'Group',
    _id: '5b8f591bfd6e99021a2f1dd1',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'Practice Group',
    profilepic: null,
    activeType: 'Group',
    _id: '5bc1dce4b9d0faf0271e0ef5',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Tue Dec 18 2018 00:00:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'Practice Group',
    profilepic: null,
    activeType: 'Group',
    _id: '5bc1dce4b9d0faf0271e0ef5',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Sat Dec 15 2018 00:00:00 GMT+0530 (IST)',
    time: '12:00',
    user_name: 'Jayna',
    profilepic: 'http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg',
    activeType: 'Individual',
    _id: '5b289024e019bb7623761bae',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Fri Dec 12 2018 05:30:00 GMT+0530 (IST)',
    time: '14:00',
    user_name: 'GCK',
    profilepic: 'http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg',
    activeType: 'Individual',
    _id: '5ae1691241ce6c991f45b7fc',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Fri Dec 11 2018 05:30:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'kaushik',
    profilepic: 'http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg',
    activeType: 'Individual',
    _id: '5b502b2724f36ada24167083',
    isCreatedByTherapist: false,
    groupMember: [] } ]

sortedName = _.sortBy(activeUser,['user_name']);

在sortedName中,我得到以下数组作为响应:

[
        {
            "date": "G",
            "data": [
                {
                    "date": "Fri Dec 12 2018 05:30:00 GMT+0530 (IST)",
                    "time": "14:00",
                    "user_name": "GCK",
                    "profilepic": "http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg",
                    "activeType": "Individual",
                    "_id": "5ae1691241ce6c991f45b7fc",
                    "isCreatedByTherapist": false,
                    "groupMember": []
                }
            ]
        },
        {
            "date": "H",
            "data": [
                {
                    "date": "Mon Dec 10 2018 00:00:00 GMT+0530 (IST)",
                    "time": "18:00",
                    "user_name": "Happy Teachers DAy",
                    "profilepic": "http://192.168.0.110:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg",
                    "activeType": "Group",
                    "_id": "5b8f591bfd6e99021a2f1dd1",
                    "isCreatedByTherapist": false,
                    "groupMember": [
                        {
                            "groupMemberId": "5b46f08262efab2027ecce7a",
                            "_id": "5b90a8385ef8155c17865afc",
                            "isMultiSession": false
                        },
                        {
                            "groupMemberId": "5b502b2724f36ada24167083",
                            "_id": "5b90a8385ef8155c17865afb",
                            "isMultiSession": false
                        },
                        {
                            "groupMemberId": "5ae1691241ce6c991f45b7fc",
                            "_id": "5b90a8385ef8155c17865af9",
                            "isMultiSession": false
                        }
                    ]
                },
                {
                    "date": "Tue Dec 11 2018 00:00:00 GMT+0530 (IST)",
                    "time": "10:30",
                    "user_name": "Happy Teachers DAy",
                    "profilepic": "http://192.168.0.110:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg",
                    "activeType": "Group",
                    "_id": "5b8f591bfd6e99021a2f1dd1",
                    "isCreatedByTherapist": false,
                    "groupMember": [
                        {
                            "groupMemberId": "5b46f08262efab2027ecce7a",
                            "_id": "5b90a8385ef8155c17865afc",
                            "isMultiSession": false
                        },
                        {
                            "groupMemberId": "5b502b2724f36ada24167083",
                            "_id": "5b90a8385ef8155c17865afb",
                            "isMultiSession": false
                        },
                        {
                            "groupMemberId": "5ae1691241ce6c991f45b7fc",
                            "_id": "5b90a8385ef8155c17865af9",
                            "isMultiSession": false
                        }
                    ]
                }
            ]
        },
        {
            "date": "J",
            "data": [
                {
                    "date": "Sat Dec 15 2018 00:00:00 GMT+0530 (IST)",
                    "time": "12:00",
                    "user_name": "Jayna",
                    "profilepic": "http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg",
                    "activeType": "Individual",
                    "_id": "5b289024e019bb7623761bae",
                    "isCreatedByTherapist": false,
                    "groupMember": []
                },
                {
                    "date": "Sat Dec 10 2018 05:30:00 GMT+0530 (IST)",
                    "time": "11:00",
                    "user_name": "Jayna",
                    "profilepic": "http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg",
                    "activeType": "Individual",
                    "_id": "5b289024e019bb7623761bae",
                    "isCreatedByTherapist": false,
                    "groupMember": []
                }
            ]
        },
        {
            "date": "P",
            "data": [
                {
                    "date": "Tue Dec 11 2018 00:00:00 GMT+0530 (IST)",
                    "time": "16:00",
                    "user_name": "Practice Group",
                    "profilepic": null,
                    "activeType": "Group",
                    "_id": "5bc1dce4b9d0faf0271e0ef5",
                    "isCreatedByTherapist": false,
                    "groupMember": []
                }
            ]
        },
        {
            "date": "K",
            "data": [
                {
                    "date": "Fri Dec 11 2018 05:30:00 GMT+0530 (IST)",
                    "time": "16:00",
                    "user_name": "kaushik",
                    "profilepic": "http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg",
                    "activeType": "Individual",
                    "_id": "5b502b2724f36ada24167083",
                    "isCreatedByTherapist": false,
                    "groupMember": []
                }
            ]
        }
    ]

如你所见,最后两个对象不合适!

请帮帮我!!!任何人的帮助将不胜感激!

回答(1)

2 years ago

使用你的数据而不做任何"group by",我确实对数组进行了排序 . 我添加了一个额外的记录,大写字母K,"Kaushik" . 您可以看到此方法首先对大写进行排序,然后对小写进行排序,但数组确实已排序 .

确保您首先获得所需的排序,然后查看任何分组的处理 .

var activeUser = [ { date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
    time: '10:30',
    user_name: 'Happy Teachers DAy',
    profilepic: 'http://192.168.0.109:8000//igXFkPIcNxSlhl0IB6c9iOexn.jpeg',
    activeType: 'Group',
    _id: '5b8f591bfd6e99021a2f1dd1',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Tue Dec 11 2018 00:00:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'Practice Group',
    profilepic: null,
    activeType: 'Group',
    _id: '5bc1dce4b9d0faf0271e0ef5',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Tue Dec 18 2018 00:00:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'Practice Group',
    profilepic: null,
    activeType: 'Group',
    _id: '5bc1dce4b9d0faf0271e0ef5',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Sat Dec 15 2018 00:00:00 GMT+0530 (IST)',
    time: '12:00',
    user_name: 'Jayna',
    profilepic: 'http://192.168.0.110:8000/D5sd646Tlx3zuOvWqhesSjIoL.jpeg',
    activeType: 'Individual',
    _id: '5b289024e019bb7623761bae',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Fri Dec 12 2018 05:30:00 GMT+0530 (IST)',
    time: '14:00',
    user_name: 'GCK',
    profilepic: 'http://192.168.0.110:8000/LDS3CpweCSSDDdOfa4lR6rPmn.jpeg',
    activeType: 'Individual',
    _id: '5ae1691241ce6c991f45b7fc',
    isCreatedByTherapist: false,
    groupMember: [] },
  { date: 'Fri Dec 11 2018 05:30:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'kaushik',
    profilepic: 'http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg',
    activeType: 'Individual',
    _id: '5b502b2724f36ada24167083',
    isCreatedByTherapist: false,
    groupMember: [] },
    { date: 'Fri Dec 11 2018 05:30:00 GMT+0530 (IST)',
    time: '16:00',
    user_name: 'Kaushik',
    profilepic: 'http://192.168.0.110:8000/DNAtiR1iiG2jgJWsIH5tpZCwv.jpeg',
    activeType: 'Individual',
    _id: '5b502b2724f36ada24167083',
    isCreatedByTherapist: false,
    groupMember: [] }
];
 
let sortedArray = _.map(_.sortBy(activeUser, ['user_name']), function(x) { return x.user_name; });

console.dir(sortedArray);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>