webworthydesign
2/19/2016 - 8:00 PM

JavaScript: sort object properties by value (numeric or string)

JavaScript: sort object properties by value (numeric or string)

// ----- URL ----- https://gist.github.com/umidjons/9614157 ----- URL -----

//Sort object properties by value (values are text)

//I have following object:

//var cities={10:'Tashkent', 14:'Karakalpakiya', 16:'Andijan'};
//I want sort it by city names, so after sort it should be:

//var cities={16:'Andijan', 14:'Karakalpakiya', 10:'Tashkent'};
//But I can't sort object properties, instead can convert object into array, then sort items.

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:

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:

//var cities={Tashkent:447, Karakalpakiya:900, Andijan:120};
//I want sort it by city rating, so after sort it should be:

//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.

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:

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)

/**
 * 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 ], ... ]
}