How to sort an array and keep track of the index in java

neteot :

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   
Alexey Malev :

Try sorting pairs of (value, index) compared by value:

public class Pair implements Comparable<Pair> {
    public final int index;
    public final int value;

    public Pair(int index, int value) {
        this.index = index;
        this.value = value;
    }

    @Override
    public int compareTo(Pair other) {
        //multiplied to -1 as the author need descending sort order
        return -1 * Integer.valueOf(this.value).compareTo(other.value);
    }
}

Then, when you're going to sort:

public static void main(String[] args) {
    Pair[] yourArray = new Pair[10];

    //fill the array
    yourArray[0] = new Pair(0, 5); yourArray[1] = new Pair(1, 10); //and so on
    Arrays.sort(yourArray);
}

Now, you have an array of Pair object ordered by value descending. Each object also contains index- the place in the original array.

P. S. I wrote the sample in Java as the question has java tag. Although, in C++ the idea is the same, only the implementation is a little bit different.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to keep track of the index of a numpy array element

how to keep update and keep track of two dimensional array in java?

How can I keep track of array index in for loop?

How to keep track of objects in an Array?

How can I sort a 2d array and keep track of the array indexes?

How to keep track of previous index in a for loop? (rust)

How do I remove the lowest n numbers in a numpy array and keep track of its index?

How do I keep track of double array items index with ramda map?

Keep track number of moves of element in array in Shell Sort C++

Angular - How to keep track of array of arrays in a FormGroup

How to use an array to keep track of different numbers?

how to keep track of objects inserted in an array

PHP sort endless array and keep the index strings

how to sort in array index to index

How to keep track of no of objects created in a java application?

How to keep track of number of objects created in java

How can I keep track of the number of pivots in Quick Sort?

How to Keep Track of colem index when JTable has been Reordering?

How to keep track of the last index in a binary search algorithm?

How to keep track of index in Double ngFor loop in angular?

How to keep sort order in a new array php

How to sort an associative array by value and keep values?

How to bubble sort an array, but keep the context of the values

Keep track of items in array with timer

Sort and keep index of a n-dimension array -- MATLAB

(Java) How to Keep Track of Numbers Entered by User for Determining Min and Max

Java MDB - How to keep track of number of messages processed

How does one keep track of all the components in a Java Application?

How do you keep track of a board game positions live? (Java)