4

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?

5
  • 1
    Could you not use a 4th array to indicate their positions? Commented Sep 17, 2014 at 0:36
  • How would I do this? Commented Sep 17, 2014 at 0:37
  • Basically, you need a proxy array, which holds the index of into the other arrays. You sort this array, based on your needs Commented Sep 17, 2014 at 0:38
  • How would I be able to sort the proxy array in any useful way, be alphabetically or largest to smallest distance? and then apply that sorting to the rest? Commented Sep 17, 2014 at 0:41
  • Recommended way would be creating a pojo with the three values and maintain List<YourPojo> and implement a Comparator and use Collections.sort Commented Sep 17, 2014 at 0:43

2 Answers 2

9

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

8

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.