How to sort an array of objects by property values ?
Last Updated :
22 May, 2024
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
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 }
]
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 }
]
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 }
]
Using external libraries like Lodash
To sort an array of objects using Lodash, first ensure Lodash is imported. Then, utilize _.sortBy(array, 'property') to sort the array by the specified property, such as age or name. This method returns a new sorted array.
Example: In this example we sorts an array of objects by the age property using Lodash's _.sortBy method. After sorting, the array elements will be ordered by age in ascending order.
JavaScript
let array = [
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 20 }
];
array = _.sortBy(array, 'age');
Output:
[
{ name: 'Charlie', age: 20 },
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 }
]
Similar Reads
How to Sort an Array of Objects by String Property value in Angular ?
An Object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method. In this article, we will learn how to Sort an array of objects by string property value in Angular. He
3 min read
How to Sort an Arrays of Object using Arrays.sort()
To sort the array of objects in both ascending and descending order in Java, we can use the Arrays.sort() method with the Comparator. In this article, we will learn how to sort an array of objects using Arrays.sort(). Sorting an Array of ObjectWith Array of Object what it really means is an array st
3 min read
Sort Array of Objects By String Property Value in JavaScript
Sorting arrays of objects based on a string property can be helpful for handling user data or dynamic lists. Here are different ways to sort an array of Objects By String Property Value.1. Using localeCompare() Method â Most UsedThe JavaScript localeCompare() method returns a number indicating wheth
3 min read
How to Sort an Array of Objects with Null Properties in TypeScript ?
Sorting an array of objects in TypeScript involves arranging objects based on specified properties. When dealing with null properties, ensure consistency by placing them either at the start or end of the sorted array to maintain predictability in sorting outcomes. Below are the approaches used to so
5 min read
Sort Array of Objects by Property with Undefined Values in TypeScript
In TypeScript, sorting an array of objects by property with undefined values by comparing the defined values precedes those with the undefined ones. Below are the approaches to sort the array of objects by property with undefined values in TypeScript: Table of Content Using Array.reduce and findInde
4 min read
Sort an array of objects using Boolean property in JavaScript
Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a
2 min read
How to Sort an Array of Object using a Value in TypeScript ?
Sorting an array of objects using a value in TypeScript pertains to the process of arranging an array of objects in a specified order based on a particular property or value contained within each object. The below approaches can be used to sort an array of objects using a value in TypeScript: Table
3 min read
How to Sort JSON Array in JavaScript by Value ?
Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
Sort an array of objects in typescript?
We can achieve the sorting in TypeScript using the sort() function which is an inbuilt TypeScript function that is used to sort the elements of an array. Below are a few examples showing the sorting in TypeScript. Example 1: In this example, we will learn how to use the sort function to sort the arr
3 min read
How to Sort Objects in an Array Based on a Property in a Specific Order in TypeScript ?
Sorting objects in an array based on a specific property is a common task in software development. TypeScript, with its static typing and powerful features, provides various approaches to accomplish this task efficiently. The below approaches can be used to sort an array of objects in properties. Ta
3 min read