1

I have two arrays:

keys_array   = [2,4,5,8,9]
values_array = ['two','four','five','eight','nine']

I need to get:

#transitional result
#values sorted alphabetically
sorted_by_value_name_array = [
  ['8','eight'],
  ['5','five'],
  ['4','four'],
  ['9','nine'],
  ['2','two']
]

#final result  
sorted_keys_array = [8,5,4,9,2]

How can i achieve this with javascript or jQuery?

3
  • I don't see any sorting pattern? Commented Sep 11, 2011 at 10:56
  • @Shef I assume he needs to combine the arrays, sort by values and return the keys of the sorted array. Commented Sep 11, 2011 at 10:58
  • values should be sorted by alphabet Commented Sep 11, 2011 at 10:58

3 Answers 3

4
var keys_array   = [2,4,5,8,9];
var values_array = ['two','four','five','eight','nine'];
var new_array = [];
for(var i=0; i<keys_array.length; i++){
    new_array.push([keys_array[i],values_array[i]]);
}

console.log(new_array.sort(function(a,b){ return a[1] < b[1] ? -1 : 1; }));
Sign up to request clarification or add additional context in comments.

Comments

3

The answer by Andy is great, but what if your keys and values are not fully aligned? Then you'll need JavaScript to be able to interpret strings into numbers.

I found a working words to numbers converter by Stephen Chapman. By utilizing this in another function we can have more or less any numerical words and combine these with the corresponding number. And they don't even have to be aligned in the first place.

// paste the toWords function here
function naturalCombine(numbers, words) {
    var numberCount = numbers.length,
        combined = [];
    for (var i=0; i < numberCount; i++) {
        var word = toWords(numbers[i]).trim(),
            found = words[words.indexOf(word)];
        if (found)
            combined.push([numbers[i], found]);
        else
            combined.push([numbers[i], "unknown"]);
    }
    return combined;
}

var keysArray = ["4", "62", "2", "94", "5", "9", "8", "87", "1674"];
var valuesArray = ["two", "one thousand six hundred seventy four", "four", 
                   "five", "eight", "nine", "eighty seven", "ninety four"];

var combined = naturalCombine(keysArray, valuesArray);

Check test case on jsFiddle

2 Comments

Yea but the example with the number-words was probably just to explain his problem and in his real code he uses some other values. :)
@Andy, probably. Hopefully someone else will have use of my answer ;)
2
var keys_array = [2,4,5,8,9],
    values_array = ['two','four','five','eight','nine'];

var svalues = values_array.slice().sort(), // copy array and sort it
    skeys = [];

for(var i = 0, s = svalues.length; i < s; i++){
    skeys.push(keys_array[values_array.indexOf(svalues[i])]);
}

Demo

You will need to extend the array prototype with indexOf() for it to work with browsers which do not support it.

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.