javascript

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}
]

Live demo

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}
]

Live Demo

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}
]

Try the above code yourself

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}
]
Was this helpful?