Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort an associative array by its values in Javascript?

I have the associative array:

array["sub2"] = 1; array["sub0"] = -1; array["sub1"] = 0; array["sub3"] = 1; array["sub4"] = 0; 

What is the most elegant way to sort (descending) by its values where the result would be an array with the respective indices in this order:

sub2, sub3, sub1, sub4, sub0 
like image 304
John Smith Avatar asked Mar 04 '11 22:03

John Smith


People also ask

Does JavaScript support associative array?

JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.

How do you sort an array of elements?

rsort() - sort arrays in descending order. asort() - sort associative arrays in ascending order, according to the value. ksort() - sort associative arrays in ascending order, according to the key. arsort() - sort associative arrays in descending order, according to the value.

How do you sort values in JavaScript?

JavaScript Array sort() The sort() sorts the elements of an array. The sort() overwrites the original array. The sort() sorts the elements as strings in alphabetical and ascending order.


1 Answers

Javascript doesn't have "associative arrays" the way you're thinking of them. Instead, you simply have the ability to set object properties using array-like syntax (as in your example), plus the ability to iterate over an object's properties.

The upshot of this is that there is no guarantee as to the order in which you iterate over the properties, so there is nothing like a sort for them. Instead, you'll need to convert your object properties into a "true" array (which does guarantee order). Here's a code snippet for converting an object into an array of two-tuples (two-element arrays), sorting it as you describe, then iterating over it:

var tuples = [];  for (var key in obj) tuples.push([key, obj[key]]);  tuples.sort(function(a, b) {     a = a[1];     b = b[1];      return a < b ? -1 : (a > b ? 1 : 0); });  for (var i = 0; i < tuples.length; i++) {     var key = tuples[i][0];     var value = tuples[i][1];      // do something with key and value } 

You may find it more natural to wrap this in a function which takes a callback:

function bySortedValue(obj, callback, context) {    var tuples = [];      for (var key in obj) tuples.push([key, obj[key]]);      tuples.sort(function(a, b) {      return a[1] < b[1] ? 1 : a[1] > b[1] ? -1 : 0    });      var length = tuples.length;    while (length--) callback.call(context, tuples[length][0], tuples[length][1]);  }    bySortedValue({    foo: 1,    bar: 7,    baz: 3  }, function(key, value) {    document.getElementById('res').innerHTML += `${key}: ${value}<br>`  });
<p id='res'>Result:<br/><br/><p>
like image 113
Ben Blank Avatar answered Sep 23 '22 23:09

Ben Blank