Search code examples
androidarrayssortingprimitive

Android - more primitive arrays sort by only one array


I did not find any solution, how to simply sort primitive arrays.

I have more primitive arrays:

public static String [] arr1={"name1","name2","name3"};
public static Double [] arr2={45.07,46.06,42.07};
public static Double [] arr3={183.95,192.94,182.94};

The only thing I want to do, is to sort the arrays according to arr3, so the other data will stay related to arr3 items.

E.g. the result shoud be:

1/ 182.94 name3 42.07
2/ 183.95 name1 45.07
3/ 192.94 name2 46.06

So just to sort it by arr3 in ascending order, keeping the other related items.

I tried more solutions, but nothing worked. I also find some solutions here, but no solution works with more arrays at once, max with 2.

If I use only this:

  Arrays.sort(arr3);

Then it will just sort the items in arr3, but the other items from other arrays doesn't fit.

Then I tried to use this:

  Arrays.sort(arr1, new Comparator<Double>() {
            public Double compare( Double o1,  Double o2) {
                return Double.compare(arr3[o1], arr3[o2]);
            }
        });

but there is only arr1 and also has error that Compare (Double, Double) in anonymous class clashes with Compare(T,T) in java.util.comparator. Attempting to use incompatible return type.

For some reason the return type requires int, not double.


Solution

  • You should create a class to encapsulate the said data. avoid creating parallel arrays as you've done as they are hard to maintain and can at a times become problematic.

    Because the said data are related, then you should create a model for it, example:

    class ClassName { // I'll let you provide a more meaningful name
        private String name;
        private double latitude;
        private double distance;
    
        public String getName() {
            return name;
        }
    
        public double getLatitude() {
            return latitude;
        }
    
        public double getDistance() {
            return distance;
        }
    
        public ClassName(String name, double latitude, double distance) {
            this.name = name;
            this.latitude = latitude;
            this.distance = distance;
        }
    }
    

    Now, spin up however many objects you need populating it with the necessary name, latitude and distance then store it into an array like so:

    ClassName[] myArray = new ClassName[]{
          new ClassName("name1",45.07, 183.95),
          new ClassName("name2",46.06, 192.94)
    };
    

    Then sorting by distance or any other property becomes so simple:

    Arrays.sort(myArray, new Comparator<ClassName>() {
          @Override
          public int compare(ClassName o1, ClassName o2) {
               return Double.compare(o1.getDistance(), o2.getDistance());
          }
    );
    

    You can then add more properties to the model as needed without having to stress with the mess parallel arrays provide.