1

I must be missing the proper term or else I'm sure I could find the answer by searching... in any case, here's what I want to do.

Through javascript, I get four variables (A, B, C, and D) that I would like to sort, and still keep track of the variable name (since it's encoded with meaning information).

Sample Data:

A = 2;
B = 1;
C = 4;
D = 3;

What I need to do now is sort them in value order (4,3,2,1) such that I can actually know the variable name ordering (C,D,A,B).

3 Answers3

5

You can keep an array of value pair objects and then simply sort that array. Of course, the array's sort method need to know how to interpret the object but that can be done by supplying a comparison function to the sort method.

First declare your array of objects:

sample_data = [
  { name: 'A', value: 2 },
  { name: 'B', value: 1 },
  { name: 'C', value: 4 },
  { name: 'D', value: 3 }
];

Then write a comparison function:

function custom_compare (a,b) {
  // I'm assuming all values are numbers
  return a.value - b.value;
}

Then simply sort and reverse:

sample_data.sort(custom_compare).reverse();

To print out the sorted names simply iterate through the array:

for (var i=0;i<sample_data.length;i++) {
  console.log(sample_data[i].name);
}
slebetman
  • 109,858
  • 19
  • 140
  • 171
  • 1
    Instead of using reverse, change the comparison to `return b.value - a.value;`. Also note that `sort` destructively sorts the array, so after the sort has run the sample_data-array is sorted. A common way to get around that is to copy the array before sorting it: `sorted_data = sample_data.slice(0).sort(custom_compare);` – some Sep 25 '12 at 04:37
  • For modern browsers (ES5): `sample_data.slice(0).sort(function(a,b){return b.value - a.value;}).forEach(function(a){console.log(a.name, a.value);});` – some Sep 25 '12 at 04:47
  • Doh! Forgot that sort does it in place. Modified the answer to remove misleading code. – slebetman Sep 25 '12 at 06:55
  • Better, but still not good enough. Why do yo still sort it in ascending order and then reverse it instead of sorting it in descending order from the start? The change is so simple: instead of `a.value - b.value` use `b.value - a.value`. – some Sep 25 '12 at 10:42
  • @some: Because that's obvious. The mistake I fixed was not obvious and someone looking at it may have been mislead. Besides, neither I nor you know if the custom_compare function will always be used in reverse. It may cause a bug at a later date for such a general purpose function to be used in an obvious manner but producing a non-obvious result. If the function was named `reverse_sort` however, it would be fine. As is, the code above is self documenting with only a minimal hit in efficiency (reverse is O(n) after all). – slebetman Sep 26 '12 at 02:47
  • It's obvious for everyone who knows about it. However it isn't them that will search and find your example and copy/paste it into their code. But lets agree that we disagree. :) – some Sep 26 '12 at 03:05
  • If they copy paste my code then I prefer for the code to do the obvious thing. That is, sort should always sort in ascending order. Doing otherwise is unexpected and may cause a bug. You can either use a comment to tell maintainers of your code that it's doing it in reverse, or you can use a descriptive function name or you can do what I do and call `.reverse()`. – slebetman Sep 26 '12 at 03:10
  • Thanks I used your code as is, and like the long route or logical sort then reversing. – Bradley M. Davis Sep 26 '12 at 03:28
0

May it help you:

https://github.com/shinout/SortedList

This is sortedlist library.

Minh Nguyen
  • 1,989
  • 3
  • 17
  • 30
0

I think what you should be looking for something like "Associative arrays" implemented in Javascript.

Check this earlier thread for your answer.

Community
  • 1
  • 1
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133