Open In App

How to Sort JSON Array in JavaScript by Value ?

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

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.

Using sort() Function

In this approach, we are using Object.values() to extract object values, sort() to sort them alphabetically, and JSON.stringify() to convert them to strings for comparison, effectively sorting the JSON array by its values.

Syntax:

obj.sort((a, b) => a - b);

Example: The below example uses the sort() function to sort JSON array in JavaScript by value.

JavaScript
const jArr = [
    { name: "Ravi", age: 30 },
    { name: "Kavi", age: 25 },
    { name: "Vijay", age: 35 }
];
jArr.sort((a, b) => {
    const aValue = JSON.
        stringify(Object.values(a).sort());
    const bValue = JSON.
        stringify(Object.values(b).sort());
    if (aValue < bValue) return -1;
    if (aValue > bValue) return 1;
    return 0;
});
console.log(jArr);

Output
[
  { name: 'Kavi', age: 25 },
  { name: 'Ravi', age: 30 },
  { name: 'Vijay', age: 35 }
]

Using Array.prototype.reduce()

In this approach, we are using Array.prototype.reduce() to iterate over the JSON array and dynamically insert each object into a sorted position based on a value comparison, achieved by using Object.values() to access object values and findIndex() to find the correct insertion point.

Syntax:

obj.reduce( myFunction, initialValue )

Example: The below example uses the Array.prototype.reduce() to sort json array in JavaScript by value.

JavaScript
const jArr = [
    { name: "Ravi", age: 30 },
    { name: "Kavi", age: 25 },
    { name: "Vijay", age: 35 }
];

const res = jArr.reduce((acc, curr) => {
    const index = acc.findIndex(
        item => Object.values(item).
        some(value => value > 
            Object.values(curr)[0]));
    if (index === -1) {
        acc.push(curr);
    } else {
        acc.splice(index, 0, curr);
    }
    return acc;
}, []);

console.log(res);

Output
[
  { name: 'Kavi', age: 25 },
  { name: 'Ravi', age: 30 },
  { name: 'Vijay', age: 35 }
]

Using Bubble Sort

In this approach, we are using Bubble Sort to iterate through the JSON array and compare the first values of each object to sort the array based on these values, assuming each object has a single key-value pair.

Example: The below example uses the Bubble Sort to sort json array in JavaScript by value.

JavaScript
const jArr = [
    { name: "Ravi", age: 30 },
    { name: "Kavi", age: 25 },
    { name: "Vijay", age: 35 }
];

for (let i = 0; i < jArr.length; i++) {
    for (let j = 0; j < jArr.length - 1 - i; j++) {
        const value1 = Object.
            values(jArr[j])[0];
        const value2 = Object.
            values(jArr[j + 1])[0];
        if (value1 > value2) {
            const temp = jArr[j];
            jArr[j] = jArr[j + 1];
            jArr[j + 1] = temp;
        }
    }
}
console.log(jArr);

Output
[
  { name: 'Kavi', age: 25 },
  { name: 'Ravi', age: 30 },
  { name: 'Vijay', age: 35 }
]


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

Similar Reads