Javascript: How to Sort a regular object based on key and value

Javascript: How to Sort a regular object based on key and value

Assume like you have an object { '1': 1, '3': 1, '4': 3, '5': 1 } and you would like to sort it by its value.

Then you can follow below way. I just added debug information to help with visulization but the function you need is simply

Sort by value

Object.entries(obj).sort(function(a, b) { return a[1] - b[1] }); 

And if you would like to sort by key then

Object.entries(obj).sort(function(a, b) { return a[0] - b[0] }); 

Debug Code

> Object.entries(obj)                                                                                                                                                                                       
[ [ '1', 1 ], [ '3', 1 ], [ '4', 3 ], [ '5', 1 ] ]                                                                                                                                                          
> Object.entries(obj)[0]                                                                                                                                                                                    
[ '1', 1 ]                                                                                                                                                                                                  
> Object.entries(obj)[0][0]                                                                                                                                                                                 
'1'                                                                                                                                                                                                         
> Object.entries(obj).sort(function(a, b) { return a[1] - b[1] });                                                                                                                                          
[ [ '1', 1 ], [ '3', 1 ], [ '5', 1 ], [ '4', 3 ] ]                                                                                                                                                          
> obj                                                                                                                                                                                                       
{ '1': 1, '3': 1, '4': 3, '5': 1 }                                                                                                                                                                          
> 
> Object.entries(obj).sort(function(a, b) { return a[0] - b[0] });                                                                                                                                          
[ [ '1', 1 ], [ '3', 1 ], [ '4', 3 ], [ '5', 1 ] ]                                                                                                                                                          
> 

Hope it helps.

Comments

Popular posts from this blog

grep: unknown device method

Uploading files to FTP/SFTP using CURL

How to find outgoing IP in Linux ?