Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting multiple arrays simultaneously

So i am making a program that needs to handle multiple arrays. Is there any way to sort all these arrays to reflect the sorting of one array? These values are at the same index position across all three arrays and need to stay at the same index value after sorting

Example:

I have three arrays:

String[] distance = [1,3,6,7,9];
String[] name = [Joel, John, Joe, Jill, Jane]
String[] values = [1.5,2.3,5.6,7.1,6.5];

Is there any way to sort the distance array and then reflect that sorting to the other arrays. So if I sort by name and Jane becomes 0, the other values at the same positions in the other arrays will also move to 0. How could I do this?

like image 270
axtscz Avatar asked Dec 03 '22 18:12

axtscz


2 Answers

A better/more object oriented approach could be to have an object holding each of the 3 fields and having it sortable on whichever field you need.

public static class MyObject implements Comparable<MyObject> {

    public int distance;
    public String name;
    public float value;


    // Replace this with whichever field is needed
    @Override
    public int compareTo(MyObject o) {
        // If it's the String
        return this.name.compareTo(o.name);
        // If it's one of the values
        return this.distance - o.distance;
    }
}
like image 136
daentech Avatar answered Dec 10 '22 15:12

daentech


Assuming that each array has a index mapping to the other, you will need a proxy array which is used to maintain that mapping, then sort this array accordingly, for example...

The mapping array acts as the master indexer, even though it's own order may change, each entry still points to the associated position in the other arrays that it represents...

String[] distance = {"1", "3", "6", "7", "9"};
String[] name = {"Joel", "John", "Joe", "Jill", "Jane"};
String[] values = {"1.5", "2.3", "5.6", "7.1", "6.5"};
// Mapping array...
Integer[] proxyLookup = new Integer[]{0, 1, 2, 3, 4};

System.out.println("Unsorted...");

for (int index : proxyLookup) {
    System.out.println(name[index] + "; " + distance[index] + "; " + values[index]);
}

Arrays.sort(proxyLookup, new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
        return name[o1].compareTo(name[o2]);
    }
});

System.out.println("Sorted...");

for (int index : proxyLookup) {
    System.out.println(name[index] + "; " + distance[index] + "; " + values[index]);
}

This will output...

Unsorted...
Joel; 1; 1.5
John; 3; 2.3
Joe; 6; 5.6
Jill; 7; 7.1
Jane; 9; 6.5

Sorted...
Jane; 9; 6.5
Jill; 7; 7.1
Joe; 6; 5.6
Joel; 1; 1.5
John; 3; 2.3

Note, that the order that the values are listed are different, but the associated data remains the same...

A simpler solution would be to encapsulate the data into a single Object which maintained the properties. This would greatly simplify the problem

like image 33
MadProgrammer Avatar answered Dec 10 '22 14:12

MadProgrammer