1

I need to sort an array according to another array, eg:

list1: [221, 54, 50, 138, 125, 145]

list2: [50, 125]

then the sorted list1 should be: [50, 125 ,221, 54, 138, 145]

I tried the following code:

Collections.sort(list1, Comparator.comparing(listItem -> list2.indexOf(listItem)));

But this results in: [221, 54, 138, 145, 50, 125]

How can I get the 50 and 125 at the top and the rest after?

2 Answers 2

1

When list1 element does not contain in list2 then indexOf return -1 that's why those values come first. So for those value use list1.size() then those value will come later.

Collections.sort(list1, Comparator.comparing(listItem -> list2.contains(listItem) ? list2.indexOf(listItem): list1.size()));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the answer, i had made the question generic but this worked even with sorting an array objects.
1

The reason why you are getting the items of list1 first is that the index of its elements which are not there in list2 is returned as -1 and therefore they are getting placed first. All you need to do is to reverse the order as shown below:

list2.sort(Comparator.reverseOrder());
Collections.sort(list1, Comparator.comparing(listItem -> list2.indexOf(listItem)).reversed());

Note that you need to sort list2 in reverse order before applying the reversed. If you do not want to change ordering or list2, you can clone list2 and use it with Comparator.

Demo:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> list1 = new ArrayList<>(List.of(221, 54, 50, 138, 125, 145));
        List<Integer> list2 = new ArrayList<>(List.of(50, 125));

        // Clone list2
        List<Integer> list2Clone = new ArrayList<>(list2);
        // Sort the cloned list in reverse order
        list2Clone.sort(Comparator.reverseOrder());

        // Sort list1
        Collections.sort(list1, Comparator.comparing(listItem -> list2Clone.indexOf(listItem)).reversed());

        // Display the result
        System.out.println(list1);
    }
}

Output:

[50, 125, 221, 54, 138, 145]

4 Comments

It seems i need to follow up, i made the above example generic, but in my app i need to sort an array of objects, and my old code: Collections.sort(roomSets, Comparator.comparing(room -> list.indexOf(room.getId()))); worked(resulting in the reverse order). But on adding the reversed() function there Collections.sort(roomSets, Comparator.comparing(room -> list.indexOf(room.getId())).reversed());, it suddenly cannot resolve the method at all.(The Id is String). Please let me know if you have an idea why the object behaves like that on adding the reversed function
@JohnMarkson - I'm busy with some meetings and I'll look into the new requirement once my meetings are over. Just wanted to confirm the new requirement: the solution works for numbers but you want a solution that can work for any type of object (e.g. String or objects of some class) - is it the correct understanding of your requirement? Maybe you can post a new question for a faster response so that other contributors can post some solution before I can look into it after my meetings are over.
Oh, Thanks for responding even though you are busy, but i got it to work using the other answer in this post itself, the other user suggested using the ternary operator to specify a high index value for the items that don't exist in list2
That's great! Wish you success!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.