Array in Order

im trying to make a method that takes in an array and makes a new array with the values from 0 to the array.length-1 so that the array it returns will corrispond with the values in the original array (if the number at the original element[ n ] is the higest number of all the array its value in the new array would be array.length-1

examples:

{81,-81,10,2,-2,-10} becomes {5,0,4,3,2,1}

{79,-17,2,0,0,-2}becomes {5,0,4,2,3,1}

{80,-80,0,0,0,0} becomes {5,0,1,2,3,4}

i tried something like this:

int[] newOrder =newint[order.length];

for(int i = 0; i < newOrder.length; i++)

newOrder[i]=0;

for(int h = 0; h < order.length;h++)

for(int g = 0; g < order.length;g++)

if(order[h]>order[g])

newOrder[h]++;

cept with that if i send it {80,-80,0,0,0,0} it comes out {5,0,1,1,1,1} which is not what i want, i want each number to be a differnt number between 0 and array.length-1

[1299 byte] By [sosleepya] at [2007-10-2 20:57:21]
# 1

Try something like this:public int[] order(final int[] array) {

int[] temp = new int[array.length];

for(int i = 0; i < array.length; i++)

temp[i] = array[i];

java.util.Arrays.sort(temp);

java.util.List list = new java.util.ArrayList();

for(int i = 0; i < array.length; i++)

list.add(new Integer(temp[i]));

temp = new int[list.size()];

for(int i = 0; i < array.length; i++) {

Integer n = new Integer(array[i]);

int index = list.indexOf(n);

temp[i] = index;

list.set(index, null);

}

return temp;

}

I didn't comment or test it, so you'll need to see for your self how (and if) it works.

Good luck.

prometheuzza at 2007-7-13 23:42:02 > top of Java-index,Other Topics,Algorithms...
# 2
works (from what i can tell so far) like a charm. thanks
sosleepya at 2007-7-13 23:42:02 > top of Java-index,Other Topics,Algorithms...