Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting object property by values

If I have a JavaScript object such as:

var list = {   "you": 100,    "me": 75,    "foo": 116,    "bar": 15 }; 

Is there a way to sort the properties based on value? So that I end up with

list = {   "bar": 15,    "me": 75,    "you": 100,    "foo": 116 }; 
like image 710
Steerpike Avatar asked Jul 01 '09 15:07

Steerpike


People also ask

How do you sort an object by property in powershell?

If you want to sort by multiple properties, separate the properties by commas. The Get-ChildItem cmdlet gets the files from the directory specified by the Path parameter. The objects are sent down the pipeline to the Sort-Object cmdlet.

How do you sort an object in an object?

const obj = { "CAB": { name: 'CBSSP', position: 2 }, "NSG": { name: 'NNSSP', position: 3 }, "EQU": { name: 'SSP', position: 1 } };

Can we sort object in JavaScript?

Sort an Array of Objects in JavaScriptTo sort an array of objects, you use the sort() method and provide a comparison function that determines the order of objects.


2 Answers

Move them to an array, sort that array, and then use that array for your purposes. Here's a solution:

var maxSpeed = {     car: 300,      bike: 60,      motorbike: 200,      airplane: 1000,     helicopter: 400,      rocket: 8 * 60 * 60 }; var sortable = []; for (var vehicle in maxSpeed) {     sortable.push([vehicle, maxSpeed[vehicle]]); }  sortable.sort(function(a, b) {     return a[1] - b[1]; });  //[["bike", 60], ["motorbike", 200], ["car", 300], //["helicopter", 400], ["airplane", 1000], ["rocket", 28800]] 

Once you have the array, you could rebuild the object from the array in the order you like, thus achieving exactly what you set out to do. That would work in all the browsers I know of, but it would be dependent on an implementation quirk, and could break at any time. You should never make assumptions about the order of elements in a JavaScript object.

var objSorted = {} sortable.forEach(function(item){     objSorted[item[0]]=item[1] }) 

In ES8, you can use Object.entries() to convert the object into an array:

const maxSpeed = {     car: 300,      bike: 60,      motorbike: 200,      airplane: 1000,     helicopter: 400,      rocket: 8 * 60 * 60 };  const sortable = Object.entries(maxSpeed)     .sort(([,a],[,b]) => a-b)     .reduce((r, [k, v]) => ({ ...r, [k]: v }), {});  console.log(sortable);

In ES10, you can use Object.fromEntries() to convert array to object. Then the code can be simplified to this:

const maxSpeed = {     car: 300,      bike: 60,      motorbike: 200,      airplane: 1000,     helicopter: 400,      rocket: 8 * 60 * 60 };  const sortable = Object.fromEntries(     Object.entries(maxSpeed).sort(([,a],[,b]) => a-b) );  console.log(sortable);
like image 71
Nosredna Avatar answered Oct 17 '22 13:10

Nosredna


We don't want to duplicate the entire data structure, or use an array where we need an associative array.

Here's another way to do the same thing as bonna:

var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};  keysSorted = Object.keys(list).sort(function(a,b){return list[a]-list[b]})  console.log(keysSorted);     // bar,me,you,foo
like image 35
Markus R Avatar answered Oct 17 '22 14:10

Markus R