
- 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
JavaScript: Sort Object of Objects
Suppose we have an Object of Objects like this −
const obj = { "CAB": { name: 'CBSSP', position: 2 }, "NSG": { name: 'NNSSP', position: 3 }, "EQU": { name: 'SSP', position: 1 } };
We are required to write a JavaScript function that takes in one such array and sorts the sub-objects on the basis of the 'position' property of sub-objects (either in increasing or decreasing order).
Example
The code for this will be −
const obj = { "CAB": { name: 'CBSSP', position: 2 }, "NSG": { name: 'NNSSP', position: 3 }, "EQU": { name: 'SSP', position: 1 } }; const sortByPosition = obj => { const order = [], res = {}; Object.keys(obj).forEach(key => { return order[obj[key]['position'] - 1] = key; }); order.forEach(key => { res[key] = obj[key]; }); return res; } console.log(sortByPosition(obj));
Output
And the output in the console will be −
{ EQU: { name: 'SSP', position: 1 }, CAB: { name: 'CBSSP', position: 2 }, NSG: { name: 'NNSSP', position: 3 } }
- Related Articles
- How to Sort object of objects by its key value JavaScript
- How to transform object of objects to object of array of objects with JavaScript?
- Converting array of objects to an object of objects in JavaScript
- Sort array of objects by string property value - JavaScript
- Sort Array of objects by two properties in JavaScript
- Sort array based on presence of fields in objects JavaScript
- Sort array of objects by string property value in JavaScript
- Sort an array of objects by multiple properties in JavaScript
- Convert object of objects to array in JavaScript
- Convert object to array of objects in JavaScript
- JavaScript Bubble sort for objects in an array
- How to Sort/Order keys in JavaScript objects ?
- JavaScript Converting array of objects into object of arrays
- Sort array according to the date property of the objects JavaScript
- Converting array of objects to an object in JavaScript

Advertisements