
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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'}, }
We are required to write a function that sorts the object according to its keys and returns it.
Example
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));
Output
The output in the console will be −
{ 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' } }
- Related Articles
- How to sort an object in ascending order by the value of a key in JavaScript?
- JavaScript - Sort key value pair object based on value?
- JavaScript: Sort Object of Objects
- Sort array of objects by string property value - JavaScript
- Sort array of objects by string property value in JavaScript
- How to group an array of objects by key in JavaScript
- Sort Python Dictionaries by Key or Value
- JavaScript: How to Create an Object from Key-Value Pairs
- Join two objects by key in JavaScript
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- Find specific key value in array of objects using JavaScript
- How to transform object of objects to object of array of objects with JavaScript?
- Sort Array of objects by two properties in JavaScript
- How to retrieve a value from MongoDB by its key name?
- How to access an object value using variable key in JavaScript?

Advertisements