Open In App

How to Sort JSON Object Arrays Based on a Key?

Last Updated : 19 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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


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

Similar Reads