RosettaCodeData/Task/Sort-an-array-of-composite-structures/JavaScript/sort-an-array-of-composite-structures-1.js
2023-07-01 13:44:08 -04:00

8 lines
298 B
JavaScript

var arr = [
{id: 3, value: "foo"},
{id: 2, value: "bar"},
{id: 4, value: "baz"},
{id: 1, value: 42},
{id: 5, something: "another string"} // Works with any object declaring 'id' as a number.
];
arr = arr.sort(function(a, b) {return a.id - b.id}); // Sort with comparator checking the id.