-1

Have seen questions about sorting an javascript object but not how to sort objects within an object.

I have a javascript object below containing multiple objects (data is much larger in reality). How can I sort everything inside "data" by values from last_name in ascending or descending order and output a new sorted "data" object?

{"data":[
    {"DT_RowId":"row_1","first_name":"Tiger","last_name":"Nixon","age":"61"},
   {"DT_RowId":"row_2","first_name":"Garrett","last_name":"Winters","age":"63"},
    {"DT_RowId":"row_3","first_name":"Ashton","last_name":"Cox","age":"66"},
    {"DT_RowId":"row_4","first_name":"Cedric","last_name":"Kelly","age":"22"},
    {"DT_RowId":"row_5","first_name":"Airi","last_name":"Satou","age":"33"}
    ],"options":[],"files":[]}
jokr1
  • 69
  • 3
  • 13
  • ` a new sorted "data" object?` -> that can be achieved with `Array.slice()` ... – Jonas Wilms Apr 30 '18 at 09:07
  • I don't see how this question is the same as the one you mentioned, since here you have an array of child objects *within* an object. – jokr1 Apr 30 '18 at 10:09
  • 1
    Here the solution is `outerObject.data.sort`, and in the linked question it is `data.sort`. I suppose that this is similar enough to consider this question a duplicate – oberlies Apr 30 '18 at 10:26

4 Answers4

1

Try lodash orderBy

_.orderBy(data.data, ['first_name'],['asc']);
swapnil2993
  • 1,384
  • 11
  • 15
0

Try this:

data.data.sort((a, b) => {
  if (a.first_name < b.first_name)
    return -1
  if (a.first_name > b.first_name)
    return 1
  return 0
})

Working here: https://jsfiddle.net/6hw777kr/

Magd Kudama
  • 3,229
  • 2
  • 21
  • 25
0

You can use Array.prototype.sort like below :-

o.data.sort(({ last_name: a }, { last_name: b }) => {
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
});

DEMO

let o = {
  "data": [{
      "DT_RowId": "row_1",
      "first_name": "Tiger",
      "last_name": "Nixon",
      "age": "61"
    },
    {
      "DT_RowId": "row_2",
      "first_name": "Garrett",
      "last_name": "Winters",
      "age": "63"
    },
    {
      "DT_RowId": "row_3",
      "first_name": "Ashton",
      "last_name": "Cox",
      "age": "66"
    },
    {
      "DT_RowId": "row_4",
      "first_name": "Cedric",
      "last_name": "Kelly",
      "age": "22"
    },
    {
      "DT_RowId": "row_5",
      "first_name": "Airi",
      "last_name": "Satou",
      "age": "33"
    }
  ],
  "options": [],
  "files": []
};
o.data.sort(({ last_name: a }, { last_name: b }) => {
    return (a < b) ? -1 : ((a > b) ? 1 : 0);
});
console.log(o);
vibhor1997a
  • 2,336
  • 2
  • 17
  • 37
  • I will go with this solution, thanks all! How would you modify the code if you wanted to sort the key "street_number" by numbers? (for example 5, 10, 105, 2454). If I use code above for numbers it would do 10, 105, 2454, 5 – jokr1 Apr 30 '18 at 09:34
  • You can do `return a-b;`. Read this : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – vibhor1997a Apr 30 '18 at 09:38
  • It works! Thanks! :))) – jokr1 Apr 30 '18 at 09:40
0

You can use the sort method of array with a custom comparator. Put your comparison logic in the function.This sorting will happen in place. If you have huge dataset then I would recommend doing this on a server for faster processing.

code:

<Your data>.data.sort(function(item1,item2){
if (item1.last_name < item2.last_name)
  return -1;
if ( item1.last_name > item2.last_name)
  return 1;
return 0;
})