0

I have two lists:

List<Double> nums = Arrays.asList(5.0, 0.9, 10.4);
List<Double> mappedNums = Arrays.asList(2.1, 0.3, 1.2);

I would like to sort nums according to mappedNums: I would like to nums to be [0.9, 10.4, 5.0], since the 0.9 is the second element of nums, and the second element of mappedNums is the smallest, and so on. How do I do that?

shakedzy
  • 2,853
  • 5
  • 32
  • 62
  • Try using TreeMap with the keys as value of mappedNums and values as nums. Then you can sort it based on the keys and form the array from the sorted map values. – Aravindhan May 16 '19 at 10:10
  • 3
    Possible duplicate of [Sorting a list based on another list's values - Java](https://stackoverflow.com/questions/4839915/sorting-a-list-based-on-another-lists-values-java) – wilmol May 16 '19 at 10:14
  • 3
    You asked the same question 30 min ago [here](https://stackoverflow.com/questions/56165647/java-comparator-comparing-not-comparing/56165995#56165995). Why do you repost ? Also I had provided a working solution. – Arnaud Denoyelle May 16 '19 at 10:18
  • 2
    Possible duplicate of [Java Comparator.comparing not comparing?](https://stackoverflow.com/questions/56165647/java-comparator-comparing-not-comparing) – Sofo Gial May 16 '19 at 10:20
  • @ArnaudDenoyelle I think it's fine. The previous question was more "why does the result differ from what I expect?", which I answered. This question is then "what approach *will* work to solve this problem?". – Michael May 16 '19 at 10:29
  • This is not the same question. Read the edit below and the given answers – shakedzy May 16 '19 at 12:00

3 Answers3

4

While others have successfully shown ways to perform this with streams etc. I'm going to boldly suggest that the precondition in the question is flawed and makes the solution harder (to read and maintain) than it should be.

The values in the two lists are related. They should be stored in the same data structure instead of in two separate lists.

Combine the values into one list that contains key-value pairs in a dedicated class. But don't name them "key" or "value" or "KeyValuePair" (the reason why standard Java doesn't have a KeyValue class is because people would immediately abuse it without any thought to code readability). Use names that describe the meaning of the values. Once you have the pair-objects in a list, it will be trivial to sort the list by either field using a comparator dedicated to the task.

Torben
  • 3,805
  • 26
  • 31
3
IntStream.range(0, nums.size())
         .mapToObj(x -> new SimpleEntry<>(x, nums.get(x)))
         .sorted(Comparator.comparingDouble(x -> mappedNums.get(x.getKey())))
         .map(Entry::getValue)
         .forEachOrdered(System.out::println);// 0.9 10.4 5.0
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • This is a nice answer but it sounds as if the OP was asking how to sort `nums` in-place. This would be a much more challenging task than your current answer. – DodgyCodeException May 16 '19 at 12:42
3

I've found this way: first sort the mappedNums, then finding their index (sorted), and using that index for getting the result from other List

List<Double> result= mappedNums.stream()
        .sorted()
        .mapToInt(mappedNums::indexOf)
        .boxed()
        .map(nums::get)
        .collect(Collectors.toList());

Output:

[0.9, 10.4, 5.0]

Michael
  • 41,989
  • 11
  • 82
  • 128
Leviand
  • 2,745
  • 4
  • 29
  • 43