Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort array of objects #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Javascript/sortArrayOfObjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//sort an array of objects by the values of the object’s properties.
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'
}
];
//sorts the employees array by ages in ascending order
employees.sort((a, b) => {
return a.age - b.age;
});
//display the employess ,useing forEach()
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.age}`);
});

employees.sort((a, b) => b.age - a.age);
//To sort the employees by ages in descending order, you just need to reverse the order in the comparison function.
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.age}`);
});

//Sort an array of objects by strings
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;
});
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName}`);
});
//Sort an array of objects by dates
employees.sort((a, b) => {
let da = new Date(a.joinedDate),
db = new Date(b.joinedDate);
return da - db;
});
employees.forEach((e) => {
console.log(`${e.firstName} ${e.lastName} ${e.joinedDate}`);
});