Skip to content

Instantly share code, notes, and snippets.

@vordan
Last active December 13, 2021 11:58
Show Gist options
  • Save vordan/55eb90bc9d1104fa2cc8854600fb1bf6 to your computer and use it in GitHub Desktop.
Save vordan/55eb90bc9d1104fa2cc8854600fb1bf6 to your computer and use it in GitHub Desktop.
Sort an Array of Objects in JavaScript. How to sort an array of objects by the values of the object’s properties
//-------------------------------------------------------
// Sort an Array of Objects in JavaScript
//
// To sort an array of objects, you use the sort() method
// and provide a comparison function that determines
// the order of objects.
//-------------------------------------------------------
//-------------------------------------------------------
// Suppose that you have an array of employee objects as follows:
let employees = [
{
firstName: 'John',
lastName: 'Doe',
age: 27,
joinedDate: 'December 15, 2017'
},
{
firstName: 'Ana',
lastName: 'Rosy',
age: 25,
joinedDate: 'January 15, 2019'
},
{
firstName: 'Zion',
lastName: 'Albert',
age: 30,
joinedDate: 'February 15, 2011'
}
];
//-------------------------------------------------------
// Sort an array of objects by numbers
// The following statement snippet sorts the employees
// array by ages in ascending order:
employees.sort((a, b) => {
return a.age - b.age;
});
// To display the employees, you use the forEach() method:
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.age}`);
});
// Ana Rosy 25
// John Doe 27
// Zion Albert 30
//-------------------------------------------------------
// To sort the employees by ages in descending order,
// you just need to reverse the order in the comparison function
employees.sort((a, b) => b.age - a.age);
//--- And the following code shows the output:
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.age}`);
});
// Zion Albert 30
// John Doe 27
// Ana Rosy 25
//-------------------------------------------------------
// The following snippet shows how to sort employees by first names
// in descending order case-insensitively:
employees.sort((a, b) => {
let fa = a.firstName.toLowerCase(),
fb = b.firstName.toLowerCase();
if (fa < fb) {
return -1;
}
if (fa > fb) {
return 1;
}
return 0;
});
//--- And the following code shows the output:
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName}`);
});
// Ana Rosy
// John Doe
// Zion Albert
//-------------------------------------------------------
// Sort an array of objects by dates
// To sort the employees by joined dates, you need to:
// - Convert the joined dates from strings to date objects.
// - Sort the employees by dates.
employees.sort((a, b) => {
let da = new Date(a.joinedDate),
db = new Date(b.joinedDate);
return da - db;
});
//--- And the following code shows the output:
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.joinedDate}`);
});
// Zion Albert Feb 15, 2011
// John Doe December 15, 2017
// Ana Rosy January 15, 2019
//-------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment