Skip to content

Custom Sorts - Objects

An array of objects has no natural order, so sorting it needs a compare function that says which of two objects comes first. The function receives two elements, a and b, and returns a number:

  • a negative number puts a before b
  • a positive number puts b before a
  • zero keeps their existing order

The sorting methods are sort(), which reorders the array in place, and toSorted(), which returns a sorted copy and leaves the original alone. Custom Sorts - Arrays covers compare functions on simple values. The examples on this page use this array:

const products = [
{ name: 'Kettle', category: 'kitchen', price: 24.99, added: '2026-03-14' },
{ name: 'desk lamp', category: 'office', price: 18.5, added: '2025-11-02' },
{ name: 'Toaster', category: 'kitchen', price: 32, added: '2026-01-20' },
{ name: 'Stapler', category: 'office', price: 6.75, added: '2026-06-09' },
];

Without a compare function, sort() converts each element to a string and compares the strings. Every plain object becomes the same string, so the order does not change:

console.log(products.toSorted().map((p) => p.name));
console.log(String(products[0]));

This prints:

[ 'Kettle', 'desk lamp', 'Toaster', 'Stapler' ]
[object Object]

For a numeric property, subtracting one value from the other gives exactly the number the compare function needs. Swapping a and b reverses the order:

const byPrice = (a, b) => a.price - b.price;
const byPriceDesc = (a, b) => b.price - a.price;
console.log(products.toSorted(byPrice).map((p) => `${p.name} ${p.price}`));
console.log(products.toSorted(byPriceDesc).map((p) => `${p.name} ${p.price}`));

This prints:

[ 'Stapler 6.75', 'desk lamp 18.5', 'Kettle 24.99', 'Toaster 32' ]
[ 'Toaster 32', 'Kettle 24.99', 'desk lamp 18.5', 'Stapler 6.75' ]

Strings cannot be subtracted. Comparing them with < and > works, but it compares character codes, so every capital letter sorts before every lower-case one. localeCompare() compares the way a reader expects, and it already returns a negative number, a positive number or zero:

const byNameCodeUnits = (a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
const byName = (a, b) => a.name.localeCompare(b.name, 'en-GB');
console.log(products.toSorted(byNameCodeUnits).map((p) => p.name));
console.log(products.toSorted(byName).map((p) => p.name));

This prints:

[ 'Kettle', 'Stapler', 'Toaster', 'desk lamp' ]
[ 'desk lamp', 'Kettle', 'Stapler', 'Toaster' ]

Turn each date into a number of milliseconds and subtract, as for any other number. Date.parse() does the conversion, and putting b first sorts the newest to the top:

const newestFirst = (a, b) => Date.parse(b.added) - Date.parse(a.added);
console.log(products.toSorted(newestFirst).map((p) => `${p.added} ${p.name}`));

This prints:

[
'2026-06-09 Stapler',
'2026-03-14 Kettle',
'2026-01-20 Toaster',
'2025-11-02 desk lamp'
]

A date-only string such as '2026-03-14' is read as midnight UTC. That does not affect the order, but if the date’s parts are needed, for example to sort by month and day regardless of year, read them with the UTC methods such as getUTCMonth(), because the local methods can give the previous day in time zones behind UTC.

To sort by one property and then break ties with another, compare the first and fall back to the second when the first gives zero. The || operator does this neatly, since zero is falsy:

const byCategoryThenPrice = (a, b) =>
a.category.localeCompare(b.category) || a.price - b.price;
console.log(products.toSorted(byCategoryThenPrice).map((p) => `${p.category} ${p.name} ${p.price}`));

This prints:

[
'kitchen Kettle 24.99',
'kitchen Toaster 32',
'office Stapler 6.75',
'office desk lamp 18.5'
]

sort() reorders the original array and returns that same array, so any other code holding the array sees the new order. toSorted(), added in ES2023 and available in current browsers and Node, returns a new array instead:

const scores = [{ v: 3 }, { v: 1 }, { v: 2 }];
const sorted = scores.toSorted((a, b) => a.v - b.v);
console.log(scores.map((s) => s.v), sorted.map((s) => s.v));
scores.sort((a, b) => a.v - b.v);
console.log(scores.map((s) => s.v));

This prints:

[ 3, 1, 2 ] [ 1, 2, 3 ]
[ 1, 2, 3 ]

Both methods are stable, which the language has required since ES2019, so elements that compare as equal keep their original order. That means two passes also give a multi-property sort, provided the first pass sorts by the tie-breaker and the second by the main property.

Steve Griffith covers this topic in his video Sorting Complex Objects in JavaScript.