Forked from umidjons/sort-object-properties-by-value.md
Created
April 22, 2019 17:26
-
-
Save erika-carvalh0/c34b9c406130e8710a4a70d881f5f16f to your computer and use it in GitHub Desktop.
Revisions
-
umidjons revised this gist
Mar 18, 2014 . 1 changed file with 30 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,3 +65,33 @@ Example: var sortedCities=sortProperties(cities); console.log(sortedCities); // [[Andijan, 120], [Tashkent, 447], [Karakalpakiya, 900]] ``` ###Improved version with additional parameter to set sort type (numeric or text) ```js /** * Sort object properties (only own properties will be sorted). * @param {object} obj object to sort properties * @param {bool} isNumericSort true - sort object properties as numeric value, false - sort as string value. * @returns {Array} array of items in [[key,value],[key,value],...] format. */ function sortProperties(obj, isNumericSort) { isNumericSort=isNumericSort || false; // by default text sort var sortable=[]; for(var key in obj) if(obj.hasOwnProperty(key)) sortable.push([key, obj[key]]); if(isNumericSort) sortable.sort(function(a, b) { return a[1]-b[1]; }); else sortable.sort(function(a, b) { var x=a[1].toLowerCase(), y=b[1].toLowerCase(); return x<y ? -1 : x>y ? 1 : 0; }); return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ] } ``` -
umidjons created this gist
Mar 18, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ ### Sort object properties by value (values are text) I have following object: ```js var cities={10:'Tashkent', 14:'Karakalpakiya', 16:'Andijan'}; ``` I want sort it by city names, so after sort it should be: ```js var cities={16:'Andijan', 14:'Karakalpakiya', 10:'Tashkent'}; ``` But I can't sort object properties, instead can convert object into array, then sort items. ```js function sortProperties(obj) { // convert object into array var sortable=[]; for(var key in obj) if(obj.hasOwnProperty(key)) sortable.push([key, obj[key]]); // each item is an array in format [key, value] // sort items by value sortable.sort(function(a, b) { var x=a[1].toLowerCase(), y=b[1].toLowerCase(); return x<y ? -1 : x>y ? 1 : 0; }); return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ] } ``` Example: ```js var sortedCities=sortProperties(cities); console.log(sortedCities); // [[16, 'Andijan'], [14, 'Karakalpakiya'], [10, 'Tashkent']] ``` ### Sort object properties by value (values are numbers) I have following object: ```js var cities={Tashkent:447, Karakalpakiya:900, Andijan:120}; ``` I want sort it by city rating, so after sort it should be: ```js var cities={Andijan:120, Tashkent:447, Karakalpakiya:900}; ``` But again I can't sort object properties, instead can convert object into array, then sort items. ```js function sortProperties(obj) { // convert object into array var sortable=[]; for(var key in obj) if(obj.hasOwnProperty(key)) sortable.push([key, obj[key]]); // each item is an array in format [key, value] // sort items by value sortable.sort(function(a, b) { return a[1]-b[1]; // compare numbers }); return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ] } ``` Example: ```js var sortedCities=sortProperties(cities); console.log(sortedCities); // [[Andijan, 120], [Tashkent, 447], [Karakalpakiya, 900]] ```