Open In App

How to sort an array of objects by property values ?

Last Updated : 20 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will try to understand how to sort an array of objects by property values in JavaScript with the help of certain examples.

Pre-requisite: Array of Objects in JavaScript

Example:

Input:
[
{ name: "Ram", age: 17 },
{ name: "Mohan", age: 30 },
{ name: "Shyam", age: 15 },
{ name: "Shyam", age: 17 },
]
Output:
[
{ name: 'Shyam', age: 15 },
{ name: 'Ram', age: 17 },
{ name: 'Shyam', age: 17 },
{ name: 'Mohan', age: 30 }
]

Explanation:

  • Pick any property and sort the object on the basis of that property’s values in other objects inside an array of objects.

There are several methods that can be used to sort an array of objects by property values

  • Using sort() with compare function
  • Using sort() method
  • Using localeCompare() for string values

Approach 1: Using sort() with compare function

Here we will use the sort() method and inside the sort method, we will explicitly define a compare method for comparing values as per the user’s need. Then inside that compare() method, we will use if-else statements in order to check property values.

Example: Below is the implementation of the above approach.

Javascript




let employees_details = [
    { name: "Ram", age: 17 },
    { name: "Mohan", age: 30 },
    { name: "Shyam", age: 15 },
    { name: "Shyam", age: 17 },
];
 
let compare = (a, b) => {
    if (a.age < b.age) {
        return -1;
    }
    if (a.age > b.age) {
        return 1;
    }
    return 0;
};
 
employees_details.sort(compare);
console.log(employees_details);


Output

[
  { name: 'Shyam', age: 15 },
  { name: 'Ram', age: 17 },
  { name: 'Shyam', age: 17 },
  { name: 'Mohan', age: 30 }
]

Approach 2: Using sort() method

This approach also uses the sort() method but unlike the previous approach here we will shorten the syntax and do all the work as inline itself. In the inline technique, we will use the ternary operator concept in order to compare two different values and then return the corresponding results.

Example: Below is the implementation of the above approach.

Javascript




let employees_details = [
    { name: "Ram", age: 17 },
    { name: "Mohan", age: 30 },
    { name: "Shyam", age: 15 },
    { name: "Shyam", age: 17 },
];
 
employees_details.sort((a, b) => (
    a.age > b.age ? 1 : b.age > a.age ? -1 : 0));
console.log(employees_details);


Output

[
  { name: 'Shyam', age: 15 },
  { name: 'Ram', age: 17 },
  { name: 'Shyam', age: 17 },
  { name: 'Mohan', age: 30 }
]

Approach 3: Using localeCompare() for string values

localeCompare() compares strings in alphabetical order. Sorting objects by a string property results in an alphabetically sorted array.

Example: The employees_details array is sorted alphabetically based on the name property using localeCompare().

Javascript




let employees_details = [
    { name: "Ram", age: 17 },
    { name: "Mohan", age: 30 },
    { name: "Shyam", age: 15 },
    { name: "Shyam", age: 17 },
];
 
employees_details.sort((a, b) =>
    a.name.localeCompare(b.name));
console.log(employees_details);


Output

[
  { name: 'Mohan', age: 30 },
  { name: 'Ram', age: 17 },
  { name: 'Shyam', age: 15 },
  { name: 'Shyam', age: 17 }
]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads