0

I have an object like this :

{tsg2b: 1, fjdlf: 0} 

Now i need to sort this object based on the value for example i need the result something like this :

{fjdlf: 0,tsg2b: 1} 
MBehtemam
  • 7,865
  • 15
  • 66
  • 108

1 Answers1

1

You have to take the object into an array and then sort the array because object cannot be sorted in java script. try the following --

var test = {tsg2b: 1, fjdlf: 0} 
var sortable = [];
for (var item in test)
      sortable.push([item, test[item]])
sortable.sort(function(a, b) {return a[1] - b[1]})
Razib
  • 126
  • 5