One list with animals:(unsorted) e.g ["Lion","Tiger","Cat", "Dog", "Cuy"]
Another list animals_select: e.g ["Cuy", "Cat"]
I'm trying to sort the animals list based on the values of the second list.
Code:
List<String> animals = new ArrayList<>();
// Adding new elements to the ArrayList
animals.add("Lion");
animals.add("Tiger");
animals.add("Cat");
animals.add("Dog");
animals.add("Cuy");
System.out.println(animals);
//["Lion","Tiger","Cat", "Dog"]
List<String> animals_select = new ArrayList<>();
animals_select.add("Cat");
animals_select.add("Cuy");
I want to get a list like this ["Cuy", "Cat", "Lion","Tiger", "Dog"]
Those in the second list should be ordered first
I have tried without success
Collections.sort(animals, new Comparator() {
public int compare(left, right) {
return Integer.compare(animals_select.indexOf(left), animals_select.indexOf(right));
}
});