2

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 Answers 3

6

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

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);
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);});
Doh! Forgot that sort does it in place. Modified the answer to remove misleading code.
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: 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).
|
0

May it help you:

https://github.com/shinout/SortedList

This is sortedlist library.

Comments

0

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

Check this earlier thread for your answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.