4

How can I order this array of objects by object value? I’m using TypeScript.

console.log(this.items);

Output:

(6) [{…}, {…}, {…}, {…}, {…}, {…}]
{id: 3, ref: "P-201721", active: 1, visible: 1, weigth: 0.3, …}
{id: 4, ref: "P-201722", active: 1, visible: 1, weigth: 0.3, …}
{id: 1, ref: "P-201710", active: 1, visible: 1, weigth: 0.5, …}
{id: 2, ref: "P-201711", active: 1, visible: 1, weigth: 0.5, …}
{id: 5, ref: "P-201831", active: 1, visible: 1, weigth: 0.2, …}
{id: 6, ref: "P-201832", active: 1, visible: 1, weigth: 0.2, …}

I tried this, but the array maintains the same order: Sort an array with arrays in it by string

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Otto
  • 4,020
  • 6
  • 35
  • 46
  • You want to sort by `id`? – JohanP Jan 10 '18 at 20:23
  • Show what you actually tried. – charlietfl Jan 10 '18 at 20:23
  • 2
    This is a duplicate of https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript – Alexandre Annic Jan 10 '18 at 20:23
  • This is definitely a duplicate. The highest voted answer to [the indicated duplicate](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) (2 minutes after the question was posted) contains *"It's easy enough to write your own comparison function"*. It shouldn't be different in TypeScript. Or if there is, it is definitely a TypeScript-only canonical question. – Peter Mortensen Dec 10 '22 at 18:21
  • 1
    *"weigth"* is a misspelling of *"[weight](https://en.wiktionary.org/wiki/weight#Noun)"*. – Peter Mortensen Dec 10 '22 at 18:27
  • That is one way to [earn the Lifeboat badge](https://stackoverflow.com/help/badges/8842/lifeboat?page=1): Answer a question that is very clearly a duplicate. – Peter Mortensen Dec 10 '22 at 18:31
  • [An answer](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value/56023953#56023953) deals specifically with TypeScript. – Peter Mortensen Dec 10 '22 at 21:09

2 Answers2

22

Sorting an array of objects can be a little tricky. You have to pass in a custom sorting function to define how you want to compare the objects. How else would .sort() know you want to sort by id? Maybe you want to sort by weight instead.

I've put together an example at https://codepen.io/anon/pen/PEReGE?editors=0012. You can replace the id references with any property if you'd like it sorted by that instead.

items.sort((a, b) => {
  if(a.id > b.id) {
    return 1;
  } else if(a.id < b.id) {
    return -1;
  } else {
    return 0;
  }
});

If you are sorting by a numeric property you can use this shorthand:

items.sort((a, b) => {
  return a.id - b.id;
});
fl0psy
  • 481
  • 1
  • 4
  • 5
3

You can use Lodash to achieve what you want. You can also use multiple sorting options by passing in the keys in the array inside the sortBy function.

var data = [
{"id": 3, "ref": "P-201721", "active": 1, "visible": 1, "weight": 0.3},
{"id": 4, "ref": "P-201722", "active": 1, "visible": 1, "weight": 0.3},
{"id": 1, "ref": "P-201710", "active": 1, "visible": 1, "weight": 0.5},
{"id": 2, "ref": "P-201711", "active": 1, "visible": 1, "weight": 0.5},
{"id": 5, "ref": "P-201831", "active": 1, "visible": 1, "weight": 0.2},
{"id": 6, "ref": "P-201832", "active": 1, "visible": 1, "weight": 0.2}
]

var sort = _.sortBy(data, ["id","weight"]);

console.log(sort);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arunmmanoharan
  • 2,535
  • 2
  • 29
  • 60