How to Sort JSON Array in JavaScript by Value ?
Last Updated :
16 Apr, 2024
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 }
]
Similar Reads
How to Sort a Dictionary by Value in JavaScript ? In JavaScript, dictionaries do not have a built-in method to sort them by their values. However, there are several approaches to achieve this functionality. Below are the possible Approaches: Table of Content Using Array.sort() with Object.entries()Using a custom sort functionUsing Array.sort() with
2 min read
How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s
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 a Multidimensional Array in JavaScript by Date ? Sorting a Multidimensional Array by date consists of ordering the inner arrays based on the date values. This needs to be done by converting the date representation into the proper comparable formats and then applying the sorting function to sort the array in ascending or descending order. Below are
2 min read
How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
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 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
Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read
Sort an Array in JavaScript This article will show you how to sort a JS array.1. Using array.sort() Method To sort an array in JavaScript, we can use array.sort() method. This method sorts the elements alphabetically in ascending order.JavaScriptlet arr = ["JS", "HTML", "CSS"]; arr.sort() console.log(arr);Output[ 'CSS', 'HTML'
3 min read
How to sort an array of objects by property values ? 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: "Shy
4 min read