Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to Sort object of objects by its key value JavaScript
Let's say, we have an object with keys as string literals and their values as objects as well like this ?
const companies = {
'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'},
'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'},
'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'},
'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'},
'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'},
'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'},
'dis n dat': {employees: 540, worth: '1m', CEO: 'Mohit Sharma'},
};
console.log("Original object keys:", Object.keys(companies));
Original object keys: [ 'landwaves ltd', 'colin & co', 'motilal biscuits', 'numbtree', 'solace pvt ltd', 'ambicure', 'dis n dat' ]
We are required to write a function that sorts the object according to its keys and returns it.
Using Object.entries() and Object.assign()
This method converts the object to key-value pairs, sorts them alphabetically, and reconstructs the object:
const companies = {
'landwaves ltd': {employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal'},
'colin & co': {employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari'},
'motilal biscuits': {employees: 975, worth: '1m', CEO: 'Rahul Gupta'},
'numbtree': {employees: 1500, worth: '1.5m', CEO: 'Jay Kumar'},
'solace pvt ltd': {employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal'},
'ambicure': {employees: 170, worth: '0.1m', CEO: 'Preetam Chawla'},
'dis n dat': {employees: 540, worth: '1m', CEO: 'Mohit Sharma'},
};
const sortKeys = (obj) => {
return Object.assign(...Object.entries(obj).sort().map(([key, value]) => {
return {
[key]: value
}
}));
};
console.log(sortKeys(companies));
{
ambicure: { employees: 170, worth: '0.1m', CEO: 'Preetam Chawla' },
'colin & co': { employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari' },
'dis n dat': { employees: 540, worth: '1m', CEO: 'Mohit Sharma' },
'landwaves ltd': { employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal' },
'motilal biscuits': { employees: 975, worth: '1m', CEO: 'Rahul Gupta' },
numbtree: { employees: 1500, worth: '1.5m', CEO: 'Jay Kumar' },
'solace pvt ltd': { employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal' }
}
Using Object.fromEntries() (Modern Approach)
A cleaner and more readable approach using Object.fromEntries():
const sortKeysByFromEntries = (obj) => {
return Object.fromEntries(
Object.entries(obj).sort()
);
};
const sortedCompanies = sortKeysByFromEntries(companies);
console.log("Sorted keys:", Object.keys(sortedCompanies));
console.log(sortedCompanies);
Sorted keys: [
'ambicure',
'colin & co',
'dis n dat',
'landwaves ltd',
'motilal biscuits',
'numbtree',
'solace pvt ltd'
]
{
ambicure: { employees: 170, worth: '0.1m', CEO: 'Preetam Chawla' },
'colin & co': { employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari' },
'dis n dat': { employees: 540, worth: '1m', CEO: 'Mohit Sharma' },
'landwaves ltd': { employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal' },
'motilal biscuits': { employees: 975, worth: '1m', CEO: 'Rahul Gupta' },
numbtree: { employees: 1500, worth: '1.5m', CEO: 'Jay Kumar' },
'solace pvt ltd': { employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal' }
}
Sorting by Object Property Values
You can also sort by nested object properties like employee count:
const sortByEmployees = (obj) => {
return Object.fromEntries(
Object.entries(obj).sort(([,a], [,b]) => a.employees - b.employees)
);
};
const sortedByEmployees = sortByEmployees(companies);
console.log("Companies sorted by employee count:");
console.log(sortedByEmployees);
Companies sorted by employee count:
{
ambicure: { employees: 170, worth: '0.1m', CEO: 'Preetam Chawla' },
'colin & co': { employees: 200, worth: '0.2m', CEO: 'Sukesh Maheshwari' },
'dis n dat': { employees: 540, worth: '1m', CEO: 'Mohit Sharma' },
'motilal biscuits': { employees: 975, worth: '1m', CEO: 'Rahul Gupta' },
'landwaves ltd': { employees: 1200, worth: '1.2m', CEO: 'Rajiv Bansal' },
numbtree: { employees: 1500, worth: '1.5m', CEO: 'Jay Kumar' },
'solace pvt ltd': { employees: 1800, worth: '1.65m', CEO: 'Arvind Sangal' }
}
Comparison
| Method | Readability | Browser Support | Performance |
|---|---|---|---|
| Object.assign() + map() | Complex | ES6+ | Good |
| Object.fromEntries() | Simple | ES2019+ | Better |
Conclusion
Use Object.fromEntries(Object.entries(obj).sort()) for the cleanest approach to sort objects by keys. For sorting by property values, use custom comparison functions in the sort() method.
Advertisements
