Sort array of objects by key value in Javascript
If you have an array of objects and you want to sort them by a key value, you can use the sort() method. The sort() method takes a callback function as an argument. The callback function takes two arguments, a and b. The sort() method will compare the a and b arguments and return a negative number if a is less than b, a positive number if a is greater than b, or 0 if a is equal to b.
Use Array.sort() function to sort an array of objects by key value
The Array.sort() function can be used to sort an array of objects by key value. This can be useful when you need to sort an array of objects by a certain criteria, such as alphabetical order or numerical value.
Sort in ascending order - by score value
const subjects = [
{ "name": "Math", "score": 81 },
{ "name": "English", "score": 77 },
{ "name": "Chemistry", "score": 87 },
{ "name": "Physics", "score": 84 }
];
// Sort in ascending order - by score
subjects.sort( (a,b) => a.score - b.score );
console.log(subjects);
or you can also write it as below:
subjects.sort((a, b) => (a.score > b.score) ? 1: -1);
Output
[
{"name":"English","score":77},
{"name":"Math","score":81},
{"name":"Physics","score":84},
{"name":"Chemistry","score":87}
]
This code is sorting the array called subjects in ascending order by the score property using Array.sort() function.
Sort in descending order - by score value
const subjects = [
{ "name": "Math", "score": 81 },
{ "name": "English", "score": 77 },
{ "name": "Chemistry", "score": 87 },
{ "name": "Physics", "score": 84 }
];
// Sort in descending order - by score
subjects.sort((a, b) => b.score - a.score);
console.log(subjects);
Or
subjects.sort((a, b) => (a.score < b.score) ? 1: -1);
Output
[
{"name":"Chemistry","score":87},
{"name":"Physics","score":84},
{"name":"Math","score":81},
{"name":"English","score":77}
]
This is an example of how to sort an array of objects in JavaScript in descending order by score.
The code defines an array of objects, each with a name and score property.
The code then sorts the array in descending order by score and logs the sorted array to the console.
Sort in ascending order by name value
const subjects = [
{ "name": "Math", "score": 81 },
{ "name": "English", "score": 77 },
{ "name": "Chemistry", "score": 87 },
{ "name": "Physics", "score": 84 }
];
// Sort in ascending order - by name
subjects.sort((a, b) => (a.name > b.name) ? 1: -1);
console.log(subjects);
Output
[
{"name":"Chemistry","score":87},
{"name":"English","score":77},
{"name":"Math","score":81},
{"name":"Physics","score":84}
]
Sort array of objects in descending order by name value
const subjects = [
{ "name": "Math", "score": 81 },
{ "name": "English", "score": 77 },
{ "name": "Chemistry", "score": 87 },
{ "name": "Physics", "score": 84 }
];
// Sort in descending order - by name
subjects.sort((a, b) => (a.name < b.name) ? 1 : -1);
console.log(subjects);
Output
[
{"name":"Physics","score":84},
{"name":"Math","score":81},
{"name":"English","score":77},
{"name":"Chemistry","score":87}
]
- Convert object {key: value} to Array [[key, value]] Javascript
- Sort array using Array.sort() in javascript
- Filter an Array using object key value in Javascript
- Finding the max/min value of an attribute in an array of objects
- Lodash - Convert paired Array to key value paired object
- Collect books from array of objects and return collection of books as an array
- Get total sum of specif attribute inside of array of objects