javascript - Sorting object property by values

ID : 1880

viewed : 113

Tags : javascriptsortingpropertiesobjectjavascript





Top 5 Answer for javascript - Sorting object property by values

vote vote

98

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);

vote vote

86

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

vote vote

80

Your objects can have any amount of properties and you can choose to sort by whatever object property you want, number or string, if you put the objects in an array. Consider this array:

var arrayOfObjects = [        {         name: 'Diana',         born: 1373925600000, // Mon, Jul 15 2013         num: 4,         sex: 'female'     },     {          name: 'Beyonce',         born: 1366832953000, // Wed, Apr 24 2013         num: 2,         sex: 'female'     },     {                     name: 'Albert',         born: 1370288700000, // Mon, Jun 3 2013         num: 3,         sex: 'male'     },         {         name: 'Doris',         born: 1354412087000, // Sat, Dec 1 2012         num: 1,         sex: 'female'     } ]; 

sort by date born, oldest first

// use slice() to copy the array and not just make a reference var byDate = arrayOfObjects.slice(0); byDate.sort(function(a,b) {     return a.born - b.born; }); console.log('by date:'); console.log(byDate); 

sort by name

var byName = arrayOfObjects.slice(0); byName.sort(function(a,b) {     var x = a.name.toLowerCase();     var y = b.name.toLowerCase();     return x < y ? -1 : x > y ? 1 : 0; });  console.log('by name:'); console.log(byName); 

http://jsfiddle.net/xsM5s/16/

vote vote

69

ECMAScript 2017 introduces Object.values / Object.entries. As the name suggests, the former aggregates all the values of an object into an array, and the latter does the whole object into an array of [key, value] arrays; Python's equivalent of dict.values() and dict.items().

The features make it pretty easier to sort any hash into an ordered object. As of now, only a small portion of JavaScript platforms support them, but you can try it on Firefox 47+.

EDIT: Now supported by all modern browsers!

let obj = {"you": 100, "me": 75, "foo": 116, "bar": 15};  let entries = Object.entries(obj); // [["you",100],["me",75],["foo",116],["bar",15]]  let sorted = entries.sort((a, b) => a[1] - b[1]); // [["bar",15],["me",75],["you",100],["foo",116]] 
vote vote

52

For completeness sake, this function returns sorted array of object properties:

function sortObject(obj) {     var arr = [];     for (var prop in obj) {         if (obj.hasOwnProperty(prop)) {             arr.push({                 'key': prop,                 'value': obj[prop]             });         }     }     arr.sort(function(a, b) { return a.value - b.value; });     //arr.sort(function(a, b) { a.value.toLowerCase().localeCompare(b.value.toLowerCase()); }); //use this to sort as strings     return arr; // returns array }  var list = {"you": 100, "me": 75, "foo": 116, "bar": 15}; var arr = sortObject(list); console.log(arr); // [{key:"bar", value:15}, {key:"me", value:75}, {key:"you", value:100}, {key:"foo", value:116}] 

Jsfiddle with the code above is here. This solution is based on this article.

Updated fiddle for sorting strings is here. You can remove both additional .toLowerCase() conversions from it for case sensitive string comparation.

Top 3 video Explaining javascript - Sorting object property by values







Related QUESTION?