4

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!

3
  • Why don't you use Arrays.sort(newTime); ? Again I don't get what are you trying to achieve. What is the use case ? Commented Sep 28, 2017 at 7:29
  • 1
    @Eran post it as an answer so that I can check it! Commented Sep 28, 2017 at 7:29
  • @ShubhenduPramanik cause the first array can be 1 7 9 and the second 20 11 3 Commented Sep 28, 2017 at 7:30

4 Answers 4

2

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

4 Comments

I have only one question how can I get the second array now?if I do String a= Arrays.toString(right); System.out.println(a); it prints the second array but its not sorted how can I get it?
Super again!THanks a lot simple and fast
Sir I have two ArrayLists of difffent types. ArrayList<Double> is the first arraylist ArrayList<ProfileClass> is the second arraylist Is it possible to do the same thing in my case? I am Trying but it is giving me error on DoubleStream..
@QaisarKhanBangash post a separate question, this is what this website is for.
1

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.

6 Comments

is it correct when there are duplicates in first array?
@mlecz No, it will not work with duplicates in the first array
Put the first array :1 5 3 Put the second 10 15 13 [1=10, 3=15, 5=13] This is what it printed what I needed was 1=10 3=13 5=15
@E99 according to the code in your question you called put (1,10), put(5,15) and put(3,13), so the map can't contain 3=15 and 5=13. Perhaps your input was different than was you think.
I put the first array and then the second so first I can also put 2 9 3 and the second 20 50 90.the output should be : 2=20 3=90 9=50 this is what I am trying to achieve sort the second based on the first array indexes
|
0

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]);
  }

Comments

0

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

Comments

Your Answer

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