Java sort second array depending on indexes of first array

Viewed 1006

I have two int arrays

int[] sum=new int[n];
int[] newTime=new int[n];
  1. First: 1 5 3
  2. Second 10 15 13

Arrays.sort(sum);

  1. Prints 1 3 5

What I want is even for the second array to be sorted with the same indexes of the

  1. first=>second : 10 13 15

I have tried with maps:

SortedMap<Integer, Integer> m = new TreeMap<Integer, Integer>();
        for(int i = 0; i < priorities.length; i++){
            m.put(sum[i],newTime[i]);
        }

It just sorts the first array only,the indexes of the second array don't change.Help is appreciated! Thank You!

4 Answers

You could do it with java-8 like this for example:

    int[] left = new int[] { 1, 5, 3 };
    int[] right = new int[] { 10, 15, 13 };

    IntStream.range(0, left.length)
            .boxed()
            .map(x -> new AbstractMap.SimpleEntry<>(left[x], right[x]))
            .sorted(Comparator.comparing(SimpleEntry::getKey))
            .forEach(System.out::println);

EDIT

to actually get the second array:

Integer[] second = IntStream.range(0, left.length)
            .boxed()
            .map(x -> new AbstractMap.SimpleEntry<>(left[x], right[x]))
            .sorted(Comparator.comparing(SimpleEntry::getKey))
            .map(SimpleEntry::getValue)
            .toArray(Integer[]::new);

Your TreeMap approach leads to what you need:

SortedMap<Integer, Integer> m = new TreeMap<Integer, Integer>();
for(int i = 0; i < priorities.length; i++){
    m.put(sum[i],newTime[i]);
}
// this will print the elements of the second array in the required order
for (Integer i : m.values()) {
    System.out.println (i);
}

Of course you can assign the elements back to the original array if you want:

int count = 0;
for (Integer i : m.values()) {
    newTime[count] = i;
}

As correctly commented by mlecz, this solution will only work if the first array (sum) has no duplicates.

Here is the solution, which works when there are duplicates in first array. You keep values from both arrays together, sort them using indices from one array, and make a list, from corresponding indices from second array.

  static Integer[] sort(int[] arr1, int[] arr2) {
    assert arr1.length == arr2.length;
    class Tuple{
      int a1;
      int a2;
    }

    List<Tuple> tuples = new ArrayList<>();
    for(int i=0;i<arr1.length;i++){
      Tuple t = new Tuple();
      t.a1=arr1[i];
      t.a2=arr2[i];
      tuples.add(t);
    }
    tuples.sort((t1,t2)->Integer.compare(t1.a1,t2.a1));

    return (Integer[]) tuples.stream().map(t->t.a2).collect(Collectors.toList()).toArray(new Integer[arr1.length]);
  }

You may sort both arrays at the same time, following the reordering of the first array like this :

int[] sum = { 1, 5, 3 };
int[] newTime = { 10, 15, 13 };

for (int i = 0; i < sum.length; i++) {
    for (int j = 0; j < sum.length; j++) {
        if (sum[i] < sum[j]) {

            int temp = sum[i];
            int temp2 = newTime[i];

            sum[i] = sum[j];
            sum[j] = temp;

            newTime[i] = newTime[j];
            newTime[j] = temp2;
        }
    }
}

System.out.println(Arrays.toString(sum));
System.out.println(Arrays.toString(newTime));
Related