Sorting Objects by Numeric Values using JavaScript
Last Updated :
17 May, 2024
Given an array of objects, our task is to sort objects by numeric values in JavaScript. An object is a complex data type in JavaScript that allows us to store collections of key-value pairs.
Example:
Input: [ { gender : 'male' , age: 30 },
{ gender : 'female' ,age: 25 },
{gender : 'male' , age: 22 } ]
Output: [ { gender: 'male', age: 22 },
{ gender: 'female', age: 25 },
{ gender: 'male', age: 30 } ]
Explanation: Numeric values are now arranged in ascending order
Below are the approaches for Sorting objects by numeric values using JavaScript:
Using the sort() method
Define an array containing objects. Call the sort() method on the array of objects. Define a comparison function as an argument to sort(). This function takes two parameters, conventionally named a and b. Inside the comparison function, subtract the age property of object b from the age property of object a. This will result in sorting the objects in ascending order based on their age property.
Example: The below example shows Sorting objects by numeric values using the sort() method.
JavaScript
// Create an array of objects
let arrayOfObjects = [
{ gender: 'male', age: 30 },
{ gender: 'female', age: 25 },
{ gender: 'male', age: 22 }
];
// Use the sort() method with a comparison function
arrayOfObjects.sort((a, b) => a.age - b.age);
console.log(arrayOfObjects);
Output[
{ gender: 'male', age: 22 },
{ gender: 'female', age: 25 },
{ gender: 'male', age: 30 }
]
Time complexity: O(n log n).
Space complexity: O(1).
Using the localCompare() method
Define an array containing objects. Call the sort() method on the array of objects. Define a comparison function as an argument to sort(). This function takes two parameters, conventionally named a and b, representing two elements from the array being compared. Inside the comparison function, use localeCompare to compare the string representations of the age properties of objects a and b.
Example: The example below shows Sorting objects by numeric values using the localCompare() method.
JavaScript
// Create an array of objects
let arrayOfObjects = [
{ gender: 'male', age: 30 },
{ gender: 'female', age: 25 },
{ gender: 'male', age: 22 }
];
// Use the sort() method with a comparison function
arrayOfObjects.sort((a, b) =>
a.age.toString().localeCompare(b.age.toString()));
console.log(arrayOfObjects);
Output[
{ gender: 'male', age: 22 },
{ gender: 'female', age: 25 },
{ gender: 'male', age: 30 }
]
Time complexity: O(n log n).
Space complexity: O(1).
Similar Reads
Sorting an Array of Binary Values using JavaScript JavaScript allows us to sort an array containing only binary values i.e. 0 and 1, below is an example to understand the problem. Example: Input array = [ 0 , 1, 0, 0 , 0, 1, 0 , 1, 0 , 1, 1]Output array = [ 0 , 0 , 0 , 0 , 0, 0 , 1 , 1, 1, 1]Below are several approaches to sorting an array of binary
2 min read
Sorting Strings with Decimal Points in JavaScript Sorting strings with decimal points in JavaScript can be done using the localeCompare method or a custom sorting function. Examples: Input: strings = ["2.5", "1.3", "3.2", "1.1", "2.1"]Output: ["1.1", "1.3", "2.1", "2.5", "3.2"]Input: ["2.7", "1.5", "3.6", "1.2", "2.1"];Output: ["1.2", "1.5", "2.1",
2 min read
Alternative Sorting of an Array using JavaScript Alternative sorting means we have to arrange the elements of an array in such a way that the first element is the first maximum and the second element is the first minimum, the third element is the second maximum, the fourth element is the second minimum, and so on.Example:Input: array = [5, 6, 2, 4
3 min read
JavaScript Program to Sort an Associative Array by its Values In Javascript, "arrays" are indexed by numerical values, while "objects" are essentially what you might refer to as associative arrays. It's important to note that "objects" do not have a guaranteed order. In this article, we will learn how can we sort an associative array by its values. Table of Co
4 min read
JavaScript Program to Sort an Array which Contain 1 to n Values In this article, we will Sort an array that contains 1 to n values in JavaScript. There are different approaches through which we can sort an array. So, we will mainly cover three different approaches.We will explore all the above methods along with their basic implementation with the help of exampl
4 min read