I am trying to sort (decreasing) an array of integers but keeping track of the original index.
I mean, for example if I have this array:
b[] = { 4, 5, 3, 5, 2 }
after using Arrays.sort(b, Collections.reverseOrder()) it turns into ( I am using Arrays.sort, because in this example b is only length 5, but in my problem the length of b could be 1 < b.length < 70
b[] = { 5, 5, 4, 3, 2 }
but I want to somehow have the original index, I mean knowing that
bOrignalIndex[] = { 1, 3, 0, 2, 4 }
I don't know if my question in clear, please ask me everything. I have this piece of code in C++ that can be helpful because it does what I want
n=4
m=5
tord[] =
[0] 0
[1] 1
[2] 2
[3] 3
ts[] =
[0] 4
[1] 5
[2] 3
[3] 5
tord[MAXT], ts[MAXT];
bool ord(int a, int b){
return ts[a] > ts[b]; }
int main(void){
for(int m, n; scanf("%d %d", &m, &n)){
bool possible = true;
FOR(i=0;i<m, i++){ // for each team
scanf("%d", ts + i); // read team size
tord[i] = i;
}
sort(tord, tord + m, ord)
The thing is after doing this, tord has the array ordered by index, that is:
tord[] =
[0] 1
[1] 3
[2] 0
[3] 2