The second type of Java sorting depending on the indices of the first array

I have two int arrays

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

Arrays.sort(sum);

  • Print 1 3 5

What I want, even for the second array to be sorted with the same indices of

  • first => second: 10 13 15

I 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 only the first array, the indices of the second array do not change. Welcome! Thank!

+4
source share
4 answers

You can do this with java-8, 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 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);
+1
source

Your approach TreeMapleads 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 elements back to the original array if you want:

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

mlecz, , (sum) .

+1

Here is a solution that works when there are duplicates in the first array. You save the values ​​from both arrays together, sort them using indices from one array, and compile a list of the corresponding indices from the 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]);
  }
0
source

You can sort both arrays at the same time, following the reordering of the first array as follows:

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));
0
source

Source: https://habr.com/ru/post/1686518/


All Articles