javaarraysalgorithmsortingmapping

How to sort two arrays with one being sorted based on the sorting of the other?


So I have encountered this problem a lot of times. Let me explain. Say I have these two arrays: A1={1,2,3,4,5,6,7,8,9,10}; and A2={1,2,3,0,2,1,1,0,0,0};. What I require is this: When I sort A2, whatever swapping and shifting of elements takes place in A2, the same should take place in A1 as well. Basically I am trying to create a Map using two arrays instead of creating an actual HashMap or HashTable.

Finally the arrays should look like this: A1={4,8,9,10,1,6,7,2,5,3}; and A2={0,0,0,0,1,1,1,2,2,3};. The corresponding values of both arrays are still the same but the data is sorted based on A2. I need a way to do this kind of sort in the fastest way possible.

Any suggestions for this?


Solution

  • Pair Class could do the trick here.

    import java.util.*;
    public class Main
    {
        static class Pair implements Comparable<Pair>
        {
            int a1;
            int a2;
            Pair (int a1, int a2) //constructor 
            {
                this.a1 = a1;
                this.a2 = a2;
            }
            public int compareTo(Pair other) //making it only compare a2 values
            {
                return this.a2 - other.a2;
            }
        }
        public static void main(String[] args) 
        {
            int[] A1 = {1,2,3,4,5,6,7,8,9,10};
            int[] A2 = {1,2,3,0,2,1,1,0,0,0};
            Pair[] pairs = new Pair[A1.length];
            for (int i = 0; i < pairs.length; i++)
            {
                pairs[i] = new Pair(A1[i], A2[i]);
            }
            Arrays.sort(pairs);
            //printing values 
            for (int i = 0; i < A1.length; i++)
            {
                System.out.print(pairs[i].a1 + " ");
            }
            System.out.println();
            for (int i = 0; i < A2.length; i++)
            {
                System.out.print(pairs[i].a2 + " ");
            }
        }
    }
    

    By making a Pair class that holds 2 variables a1 and a2, you can override the compareTo method to only compare the a2 value, so that when Arrays.sort is called, the pairs in the Pair array will be swapped only according to the a2 values. You can then access the values in the pairs and print them out. This will produce your desired output.