HOWTO · JavaScript
How to Sort Array of Object by Property in JavaScript
This tutorial demonstrates how to sort an array of objects by some property.
On this page
To sort an array of objects by one of their properties, pass a comparator to sort(). The comparator reads that property from two objects and returns a negative number when the first should come earlier, a positive number when it should come later, or zero when their keys are equal. Use string comparison for string keys and subtraction for numeric keys.
Sort an array of objects by a string property
A comparator can normalize two strings before comparing them. This makes the example case-insensitive while preserving each original lastName value in the objects.
Run this example:
const students = [
{ firstName: 'Rohan', lastName: 'Dalal', age: 19 },
{ firstName: 'Zain', lastName: 'Ahmed', age: 17 },
{ firstName: 'Anadi', lastName: 'Malhotra', age: 19 }
];
students.sort((a, b) => {
const left = a.lastName.toLowerCase();
const right = b.lastName.toLowerCase();
if (left < right) return -1;
if (left > right) return 1;
return 0;
});
console.log(students.map(student => student.lastName).join(', '));
Ahmed, Dalal, Malhotra
The comparator returns the ordering signal that sort() needs; it does not need to return exactly -1 or 1, only a negative value, zero, or a positive value. Lowercasing is useful when capitalization should not affect the order. It does not change the stored names because toLowerCase() returns a new string.
Sort strings with localeCompare()
For string properties, localeCompare() gives a concise comparator and accounts for language-sensitive ordering rules. It returns a negative number, zero, or a positive number, so it fits directly into sort():
const students = [
{ firstName: 'Rohan', lastName: 'Dalal', age: 19 },
{ firstName: 'Zain', lastName: 'Ahmed', age: 21 },
{ firstName: 'Anadi', lastName: 'Malhotra', age: 16 }
];
students.sort((a, b) => a.lastName.localeCompare(b.lastName));
console.log(students.map(student => student.lastName).join(', '));
Ahmed, Dalal, Malhotra
Choose this approach when the key is text and locale-aware comparison is appropriate. localeCompare() compares strings; it is not a numeric sorting method. For an explicitly chosen language or options such as case sensitivity, pass the locale and options supported by the runtime to localeCompare().
Sort by a numeric property
Subtract the second object’s numeric key from the first object’s key. If the first age is smaller, a.age - b.age is negative, which places a before b in ascending order.
const students = [
{ firstName: 'Rohan', lastName: 'Dalal', age: 19 },
{ firstName: 'Zain', lastName: 'Ahmed', age: 21 },
{ firstName: 'Anadi', lastName: 'Malhotra', age: 16 }
];
students.sort((a, b) => a.age - b.age);
console.log(students.map(student => student.age).join(', '));
16, 19, 21
This subtraction pattern is for numeric values. Do not use it for names: subtracting strings does not express their alphabetical order. Likewise, passing no comparator does not make sort() inspect an object property; its default order compares string representations of array elements, so objects generally need an explicit comparator.
Sort in descending order
Reverse the comparator operands to invert its ordering. For numeric ages, b.age - a.age puts larger ages first. This keeps the comparison rule attached to the selected property and avoids sorting in one direction only to reverse the resulting array afterward.
const students = [
{ firstName: 'Rohan', lastName: 'Dalal', age: 19 },
{ firstName: 'Zain', lastName: 'Ahmed', age: 21 },
{ firstName: 'Anadi', lastName: 'Malhotra', age: 16 }
];
students.sort((a, b) => b.age - a.age);
console.log(students.map(student => student.age).join(', '));
21, 19, 16
For descending string order, reverse the result of the string comparator, for example by negating a.lastName.localeCompare(b.lastName). Keep the comparator consistent so equal keys still return zero.
Important sort() behavior and limits
sort() changes the array in place and returns that same array. If the original order must remain available, copy the array first with const sortedStudents = [...students] and sort the copy. The objects themselves are not cloned, so sorting changes their positions in the array, not their properties.
Without a comparator, sort() converts values to strings and compares those strings; this is why default sorting is not suitable for numeric ordering or for selecting a property on objects. Supply a comparator that matches the key’s type. These examples assume each object has the named string or numeric property. They do not define a policy for missing keys or mixed types, so validate such data or decide how those values should compare before applying the comparator.
For the underlying array API, see the sort() method reference.