Sorting Objects By Key/Value Pairs in JavaScript

Objects are pretty handy data structures that consist of a key and value pairs. What they can contain can differ however, a key can be a symbol or a string while the value can be any data type.

“animals” is the object. The animal names are the “key” while the integers are the “value”

Sometimes there may be some occurrences where you want to sort your object by the key or value but unfortunately there are no built-in JavaScript methods to sort t by object properties.

Up above we have an object called animals. The keys are strings which serves as the type of animal. Where as the value is an integer which represents the number of specified animal. There are 3 object methods that can be used to shift through the given data.


This method returns an array of a given object’s enumerable property pairs.

With the following method, the object gets converted to an array so now it can be more easily manipulated to get specified information from the given object. For an example, if we wanted to find which animal had the highest number, we can use a sort method.

This will order the array in descending order. b[1] — a[1] will sort the array by comparing the values.

Now we have a sorted array of arrays and we can easily get the pair with the highest key value.


This method will return an array a given object’s keys.


This method will return an array a given object’s values.

This method is useful where you only care for the value portion of the object. An example being finding the highest, lowest or most frequent values.

The methods listed above can all convert a given object into an array, allowing us to use built-in JavaScript methods to get the specified data we want.

Full Stack Software Engineering Student at Flatiron School New York City