0

If I have an object with the following properties/values: {2: 4, 4: 2, 6: 3}

How can I return a list of the properties in order of their value, so I would like to return {4: 2, 6: 3, 2: 4 }

Thanks for your help!

user3452572
  • 63
  • 1
  • 4

1 Answers1

0

Thanks for the suggestions but they didn't work. I came across this that solved the issue:

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],
            y=b[1];
        return x>y ? -1 : x<y ? 1 : 0;
    });
    return sortable; // array in format [ [ key1, val1 ], [ key2, val2 ], ... ]
}

Source: https://gist.github.com/umidjons/9614157

user3452572
  • 63
  • 1
  • 4