Object sorting arranges a collection of properties in a specific order. It can be sorted by its properties/attributes. More specifically, sorting objects by value typically involves sorting the key-value pairs of an object based on the values of a specific key of an object.
This blog will define the procedure for sorting the object by value in JavaScript.
How to Sort JavaScript Objects by Value?
To sort the JavaScript object by value, use the following methods:
Method 1: Sort JavaScript Object by Value Using “object.entries()” Method With “sort()” Method
Use the “object.entries()” method with the “sort()” method to sort the objects by value in JavaScript. The object.entries() method gives an array of a given object’s enumerable properties in [key, value] pairs. The sort() method is utilized to sort the array elements in place/order. Here, we will use the sort() method with the comparison function to compare values.
Example
Create an object named “obj” with five properties and their respective values:
UAE: 6,
Canada: 9,
Austria: 3,
Torentto: 5,
Bangladesh: 2
};
Call the “object.entries()” method for converting the object to an array that contains pairs of keys and values. Then, use the “sort()” method on this array by passing a comparison function that compares the second element, the values of properties of each pair. The array will be sorted in ascending order based on the values:
Print the sorted arrays on the console:
The output displays the sorted arrays of a given object’s enumerable properties in [key, value] pairs:
The above method gives the object’s properties in an array of key-value pairs. So, to get the object from an array, follow the below-given method.
Method 2: Sort JavaScript Object by Value Using “fromEntries()” Method
Use the “fromEntries()” method that allows the creation of an object from an array of key-value pairs. It is the new method introduced in ECMAScript 2019 (ES10).
Example
Call the “fromEntries()” method and pass the variable “sortedObject” that stores the sorted arrays of a given object’s enumerable properties in [key, value] pairs. It will convert the arrays to an object:
Print the sorted object on the console using the “console.log()” method:
As you can see that the sorted arrays have been successfully converted to the sorted object:
Method 3: Sort JavaScript Object by Value Using “reduce()” Method
You can also use the “reduce()” method with “object.entries()” and the “sort()” method to get the sorted object by value from an array of key-value pairs.
Example
Call the reduce() method to convert the sorted array back into an object by iterating over each [key, value] pair and adding it to a new object “sortObjectbyValues”:
...obj, [key]: value
}), {});
Output
That’s all about sorting JavaScript objects by value.
Conclusion
For sorting objects by value in JavaScript, use the “object.entries()” method with the “sort()” method, “fromEntries()” method, or the “reduce()” method. The “object.entries()” method gives the sorted arrays of the given objects’ enumerable properties, while the “fromEntries()” method and the “reduce()” method convert the sorted arrays to an object. This blog demonstrated the procedure for sorting the object by value in JavaScript.