-3

I have following array, my objective is sort the array based on address._id field of each object, I tried lodash orderby function but didnt get desired result.

[{
        rider_ids: '5b7116ea3dead9870b828a1a',
        is_dropoff: false,
        address: {
            type: 'home',
            city: 'Greater Noida',
            _id: '5b6d62524b4dc03c478ec129'
        }
    },
    {
        rider_ids: '5b7116ea3dead9870b828a1a',
        is_dropoff: true,
        address: {
            type: 'school',
            city: 'Noida',
            _id: '5b76d0631af1adaeabb44100'
        }
    },
    {
        rider_ids: '5b7116ea3dead9870b828a3w',
        is_dropoff: true,
        address: {
            type: 'school',
            city: 'Noida',
            _id: '5b76d0631af1adaeabb44188'
        }
    },
    {
        rider_ids: '5b7116ea3dead9870b828a8a',
        is_dropoff: true,
        address: {
            type: 'school',
            city: 'Noida',
            _id: '5b76d0631af1adaeabb44122'
        }
    }
]
Dinesh Nagar
  • 768
  • 2
  • 11
  • 23
  • 1
    The downvotes are probably because you didn't show us what you tried. The objective here is for others to help you with your code not to write it all for you – charlietfl Nov 12 '18 at 11:46
  • 1
    Because you write everything in bold and aren't showing any effort, nor are you sharing the desired result – baao Nov 12 '18 at 11:46

1 Answers1

4

A very simple sort

 const foo = [{
            rider_ids: '5b7116ea3dead9870b828a1a',
            is_dropoff: false,
            address: {
                type: 'home',
                city: 'Greater Noida',
                state: 'Uttar Pradesh',
                full: 'Amrapali Leisure Valley, Greater Noida, Uttar Pradesh, India',
                lat: 28.5887034,
                lng: 77.4271665,
                latLng: '28.5887034,77.4271665',
                _id: '5b6d62524b4dc03c478ec129'
            }
        },
        {
            rider_ids: '5b7116ea3dead9870b828a1a',
            is_dropoff: true,
            address: {
                type: 'school',
                city: 'Noida',
                state: 'Uttar Pradesh',
                zipCode: '201301',
                full: 'B-38 C/2, Sector 57, Noida, Uttar Pradesh 201301, Noida, Uttar Pradesh 201301, India',
                lat: 28.6051165,
                lng: 77.3546077,
                latLng: '28.6051165,77.3546077',
                _id: '5b76d0631af1adaeabb44100'
            }
        }
    ]
    
console.log(foo.sort((a,b) => a.address._id.localeCompare(b.address._id)));
baao
  • 71,625
  • 17
  • 143
  • 203