Wednesday, 28 August 2013

Sort an array based on an index array in Java (similar to C#'s Array.Sort())

Sort an array based on an index array in Java (similar to C#'s Array.Sort())

I am trying to find a solution in Java to do the following.
int [] keys = new int[]{7,9,4,2,8,5,6,0}; // not necessarily a continuous
series
double [] vals = new double[]{10,31,20,22,21,30,33,34}; // same length as
keys<br/>
I need to sort the keys (low to high) and arrange the corresponding vals
in that order. For example output for this case will be,
sorted keys: 0, 2, 4, 5, 6, 7, 8, 9
ordered vals: 34, 22, 20, 33, 10, 30, 21, 31
I cannot use a map as in some computations I need to access keys and
values giving an index like keys[i]or vals[j].
Thanks in advance,

No comments:

Post a Comment