How To Sort The Keys Of An Object In JavaScript

Sort the Keys of an Object in JavaScript

Suppose you are looking for a method to sort the keys of an object in JavaScript. Congratulation, we have already tested effectively by using the sort() and reduce() methods. Read till the end of the article to see how we use it.

Sort The Keys Of An Object In JavaScript

Using the sort() method

JavaScript offers you the sort() function to sort the elements of an array in place and returns the reference to the same array. The sort() overwrites the original array. And sort the elements as strings in alphabetical and ascending order.

array.sort(compareFunction)

Code example:

To sort the keys of an object in JavaScript, we just use the sort() method in Javascript.

First of all, we get the keys of an object by using Object.keys(bestPlayer).

Then, we call the sort() method to sort all keys of an object as a string in alphabetical and ascending order.

Let’s see the code example below.

let bestPlayer = {
	name: "Cristiano Ronaldo",
	age: 37,
	nationality: "Portugal",
	salary: "26,8 million GBP",
};

// Getting the keys of object
const Objkeys = Object.keys(bestPlayer);
console.log("List key of Object: ", Objkeys);

// Sorting key of object by alphabet characters
const sorted = Objkeys.sort();
console.log("After sorting the Keys of an Object: ", sorted);

Output:

List key of Object:  (4) ['name', 'age', 'nationality', 'salary']
After sorting the Keys of an Object:  (4) ['age', 'name', 'nationality', 'salary']

Example 2:

In this example, we will teach you how to sort an array of objects using keys that are available in that object.

First of all, we create the function sortByKey to sort the array of objects using object keys. In this function, we call the reduce() method to reduce the list back down to an object to get the desired result. And sorting the object keys by calling the sort() method.

let bestPlayer = {
	name: "Cristiano Ronaldo",
	age: 37,
	nationality: "Portugal",
	salary: "26,8 million GBP",
};

console.log(bestPlayer);

// Sort array of object using object keys
const sortByKey = (obj) => Object.keys(obj).sort()
	.reduce((results, key) => ((results[key] = obj[key]), results), {});

let Sorted = sortByKey(bestPlayer);
console.log("After sorting an array of object using object keys: ", Sorted);

Output:

{name: 'Cristiano Ronaldo', age: 37, nationality: 'Portugal', salary: '26,8 million GBP'}
After sorting an array of object using object keys  {age: 37, name: 'Cristiano Ronaldo', nationality: 'Portugal', salary: '26,8 million GBP'}

Browser Support

Sort() is an ECMAScript1 (ES1) feature.

This feature is supported in all browsers, and you can use it for all browsers.

Summary

In this tutorial, we have explained how to sort the keys of an object in Javascript by using the sort() method. That’s what I would like to introduce to you. Hope the article is helpful to you. If you have any comments on the article, leave me a comment below. Thank you for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *