Sort an array based on another array of integers

Suppose I have an array: [0,3,4,2,5,1] .

What I want to do is sort the array, for example:

 ["one", "two", "three", "four", "five", "six"] 

So the order corresponds to the first array.

This will be the conclusion:

 ["one", "four", "five", "three", "six", "two"] 

Is there an easy way to do this?

+16
javascript sorting arrays integer order
Oct 28 '10 at 20:35
source share
7 answers

You can do something like this:

 function getSorted(arr, sortArr) { var result = []; for (var i = 0; i < arr.length; i++) { console.log(sortArr[i], arr[i]); result[i] = arr[sortArr[i]]; } return result; } var arr = ["one", "two", "three", "four", "five", "six"]; var sortArr = [0, 3, 4, 2, 5, 1]; alert(getSorted(arr, sortArr)); 

Note: this assumes that the transferred arrays are equivalent in size, you need to add additional checks if this is not the case.

+19
Oct 28 '10 at 20:37
source share
 orderedArray= function(arr,order){ return order.map(function(itm){return arr[itm]}); } var sequence= [0, 3, 4, 2, 5, 1],arr=["one","two","three","four","five","six"] arr=new orderedArray(arr,sequence); /* returned value: (Array) one,four,five,three,six,two */ 

// You can make the order an unindexed property of the array, // and call array.ordered ()

 Array.prototype.ordered= function(order){ var arr= this; order=order || this.order; return order.map(function(itm){ return arr[itm]; }); } var arr= ["one","two","three","four","five","six"], sequence= [0, 3, 4, 2, 5, 1]; arr.order=sequence; arr.ordered() /* returned value: (Array) one,four,five,three,six,two */ 
+1
Oct 28 2018-10-28
source share

I was asked about this in a telephone interview. Then do this without creating another array, assuming the array is very large. I don't know if this is the answer, since I couldn't do it on a call (hell!), But here's what I came up with.

 var my_obj_array = ['a', 'b', 'c', 'd']; var my_indicies = [3, 1, 0, 2]; // desired result ['d', 'b', 'a', 'c'] var temp = {}; for (var i = 0; i < my_indicies.length; i++) { temp[i] = my_obj_array[i]; // preserve var j = my_indicies[i]; if (j in temp) { my_obj_array[i] = temp[j]; delete temp[j]; } else { my_obj_array[i] = my_obj_array[j]; } } 

http://jsfiddle.net/innerb/RENjW/

+1
Apr 13 '13 at 5:25
source share

I don't know how you get your first array, but you can use an array of objects instead of [0,3,4,2,5,1] :

 var arr = [ {n:0, s:'one'}, {n:3, s:'four'}, {n:4, s:'five'}, {n:2, s:'three'}, {n:5, s:'six'}, {n:1, s:'two'} ] 

And avoid handling it.

0
Oct 28 '10 at 21:06
source share

You can use the first array as a directory to sort the second using the map method:

 const firstArray = [0, 3, 4, 2, 5, 1]; const secondArray = ['one', 'two', 'three', 'four', 'five', 'six']; const result = firstArray.map((item) => { return secondArray[item]; }); // result = ["one", "four", "five", "three", "six", "two"] 
0
Apr 08 '19 at 13:27
source share
 let inds = [0,3,4,2,5,1]; let x = ["one", "two", "three", "four", "five", "six"]; let x2 = []; //Output inds.forEach(ind => x2.push(x[ind])); x2 //(6) ["one", "four", "five", "three", "six", "two"] 
0
May 08 '19 at 23:11
source share
 class test1 { public static String[] sort(int[] array,String[] str) { String[] out=new String[str.length]; for(int i=0;i<str.length;i++) { out[i]=str[array[i]]; } return out; } } 
-one
Jan 19 '11 at 7:00
source share



All Articles