Sort array of object in javascript [duplicate]












-4















This question already has an answer here:




  • Sort array of objects by string property value

    39 answers




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'
}
}
]









share|improve this question















marked as duplicate by bambam, Jorge Fuentes González, chŝdk, Community Nov 13 '18 at 0:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • why peoples are downvoting the question instead of answering it.
    – Dinesh Nagar
    Nov 12 '18 at 11:44






  • 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
    – bambam
    Nov 12 '18 at 11:46






  • 1




    thanks for feedback, I will take care about it and edit the question.
    – Dinesh Nagar
    Nov 12 '18 at 11:47










  • @bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
    – Dinesh Nagar
    Nov 12 '18 at 11:56


















-4















This question already has an answer here:




  • Sort array of objects by string property value

    39 answers




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'
}
}
]









share|improve this question















marked as duplicate by bambam, Jorge Fuentes González, chŝdk, Community Nov 13 '18 at 0:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • why peoples are downvoting the question instead of answering it.
    – Dinesh Nagar
    Nov 12 '18 at 11:44






  • 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
    – bambam
    Nov 12 '18 at 11:46






  • 1




    thanks for feedback, I will take care about it and edit the question.
    – Dinesh Nagar
    Nov 12 '18 at 11:47










  • @bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
    – Dinesh Nagar
    Nov 12 '18 at 11:56
















-4












-4








-4








This question already has an answer here:




  • Sort array of objects by string property value

    39 answers




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'
}
}
]









share|improve this question
















This question already has an answer here:




  • Sort array of objects by string property value

    39 answers




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'
}
}
]




This question already has an answer here:




  • Sort array of objects by string property value

    39 answers








javascript node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 11:54

























asked Nov 12 '18 at 11:42









Dinesh Nagar

4732921




4732921




marked as duplicate by bambam, Jorge Fuentes González, chŝdk, Community Nov 13 '18 at 0:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by bambam, Jorge Fuentes González, chŝdk, Community Nov 13 '18 at 0:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • why peoples are downvoting the question instead of answering it.
    – Dinesh Nagar
    Nov 12 '18 at 11:44






  • 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
    – bambam
    Nov 12 '18 at 11:46






  • 1




    thanks for feedback, I will take care about it and edit the question.
    – Dinesh Nagar
    Nov 12 '18 at 11:47










  • @bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
    – Dinesh Nagar
    Nov 12 '18 at 11:56




















  • why peoples are downvoting the question instead of answering it.
    – Dinesh Nagar
    Nov 12 '18 at 11:44






  • 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
    – bambam
    Nov 12 '18 at 11:46






  • 1




    thanks for feedback, I will take care about it and edit the question.
    – Dinesh Nagar
    Nov 12 '18 at 11:47










  • @bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
    – Dinesh Nagar
    Nov 12 '18 at 11:56


















why peoples are downvoting the question instead of answering it.
– Dinesh Nagar
Nov 12 '18 at 11:44




why peoples are downvoting the question instead of answering it.
– Dinesh Nagar
Nov 12 '18 at 11:44




1




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






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




1




Because you write everything in bold and aren't showing any effort, nor are you sharing the desired result
– bambam
Nov 12 '18 at 11:46




Because you write everything in bold and aren't showing any effort, nor are you sharing the desired result
– bambam
Nov 12 '18 at 11:46




1




1




thanks for feedback, I will take care about it and edit the question.
– Dinesh Nagar
Nov 12 '18 at 11:47




thanks for feedback, I will take care about it and edit the question.
– Dinesh Nagar
Nov 12 '18 at 11:47












@bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
– Dinesh Nagar
Nov 12 '18 at 11:56






@bambam as you asked about showing the effort or changes that I tried at my end , I used lodash order by function as following _.orderBy(response.rides, 'address._id', 'asc'); but it didnt work out as per my expectation and results wasnt sorted. I might be doing something wrong with it.
– Dinesh Nagar
Nov 12 '18 at 11:56














1 Answer
1






active

oldest

votes


















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)));








share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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)));








    share|improve this answer


























      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)));








      share|improve this answer
























        4












        4








        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)));








        share|improve this answer












        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)));








         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)));





         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)));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 11:44









        bambam

        44.5k873116




        44.5k873116















            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."