How to Sort JSON Object Arrays Based on a Key?
Last Updated :
19 Apr, 2024
Sorting is the process of arranging elements in a specific order. In JavaScript, we can sort a JSON array by key using various methods, such as the sort() function. Along with this, we can use various other approaches and methods that are implemented in this article.
Below are the approaches to sort JSON Object Arrays based on a Key:
Using sort() Function
In this approach, we are using the sort() function in JavaScript to sort a JSON object array (jData) based on the 'name' key. The sorting is done by comparing the 'name' values of each object using a comparator function within the sort() method.
Syntax:
obj.sort((a, b) => a - b);
Example: The below example uses the sort() function to sort JSON Object Arrays based on a Key.
JavaScript
let jData = [
{ name: "GeeksforGeeks", est: 2009 },
{ name: "Google", est: 1998 },
{ name: "Microsoft", est: 1975 }
];
jData.sort((a, b) => (a.name > b.name ? 1 : -1));
console.log(jData);
Output[
{ name: 'GeeksforGeeks', est: 2009 },
{ name: 'Google', est: 1998 },
{ name: 'Microsoft', est: 1975 }
]
Using Array.prototype.reduce()
In this approach, we are using Array.prototype.reduce() in JavaScript to sort a JSON object array (jData) based on the 'est' key. The reduce() method iteratively inserts each object into the correct position in a new array (res) by comparing the 'est' values and sorting the array based on the 'est' key.
Syntax:
obj.reduce(myFunction, initialValue)
Example: The below example uses the Array.prototype.reduce() to sort JSON Object Arrays based on a Key.
JavaScript
let jData = [
{ name: "GeeksforGeeks", est: 2009 },
{ name: "Google", est: 1998 },
{ name: "Microsoft", est: 1975 }
];
let res = jData.reduce((acc, curr) => {
let ind = acc.findIndex((item) => item.est > curr.est);
if (ind === -1) ind = acc.length;
acc.splice(ind, 0, curr);
return acc;
}, []);
console.log(res);
Output[
{ name: 'Microsoft', est: 1975 },
{ name: 'Google', est: 1998 },
{ name: 'GeeksforGeeks', est: 2009 }
]
Using Bubble Sort
In this approach, we are using Bubble Sort in JavaScript to sort a JSON object array (jData) based on the 'name' key. The nested loops in JS compare adjacent objects by their 'name' values and swap them if necessary, iterating through the array until it's sorted in ascending order based on 'name'.
Example: The below example uses the Bubble Sort to sort JSON Object Arrays based on a Key.
JavaScript
let jData = [
{ name: "GeeksforGeeks", est: 2009 },
{ name: "Google", est: 1998 },
{ name: "Microsoft", est: 1975 }
];
for (let i = 0; i < jData.length - 1; i++) {
for (let j = 0; j < jData.length - i - 1; j++) {
if (jData[j].name > jData[j + 1].name) {
let temp = jData[j];
jData[j] = jData[j + 1];
jData[j + 1] = temp;
}
}
}
console.log(jData);
Output[
{ name: 'GeeksforGeeks', est: 2009 },
{ name: 'Google', est: 1998 },
{ name: 'Microsoft', est: 1975 }
]
Similar Reads
How to Sort an Array of Objects Based on a Key in JavaScript ? In JavaScript, sorting an array of objects based on the key consists of iterating over the array, applying the sort() method or other approaches, and arranging the array in the desired sorting order. Table of Content Using sort() methodUsing Custom Sorting FunctionUsing Lodash _.orderBy() MethodUsin
3 min read
JavaScript - Sort JS Arrays of Objects by Date Key The following approaches can be used to sort the array of objects by Date as KeyUsing Array.sort() with Comparison FunctionUse the comparison function to sort the array of objects (complex array).JavaScriptconst arr = [ { name: 'Geeks', date: '2022-03-15' }, { name: 'Abc', date: '2022-03-12' }, { na
3 min read
How to render an Object in a Sorted order based upon Key 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. To achieve this, we can display the object's properties in a particular order, where the order is dete
3 min read
How to sort an array of object by two fields in JavaScript ? We have given an array of objects and the task is to sort the array of elements by 2 fields of the object. There are two methods to solve this problem which are discussed below: Approach 1:First compare the first property, if both are unequal then sort accordingly.If they are equal then do the same
3 min read
How to Sort an Array based on User Input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will see how to sort an array based o
3 min read