We have such a test code. It is assumed that the created classes should be sorted in the first case ( LegComparator ) by the largest number of legs, and in the second case ( TailComparator ) by the presence of tails.
In both cases, the conclusion (as I thought) should be: first the dogs, and then the kiwi.
However, it turns out the opposite. What important thing did I miss?
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Test { public static void main(String[] args) { ArrayList<Animal> any = new ArrayList<>(); any.add(new Kiwi()); any.add(new Dog()); any.add(new Kiwi()); any.add(new Dog()); Collections.sort(any, new LegComparator()); for (Animal animal : any) { System.out.println(animal); } System.out.println(); Collections.sort(any, new TailComparator()); for (Animal animal : any) { System.out.println(animal); } } static class Animal { protected int countLeg = 2; protected boolean tail = false; } static class Dog extends Animal { public Dog() { countLeg = 4; tail = true; } @Override public String toString() { return "Dog"; } } static class Kiwi extends Animal { public Kiwi() { } @Override public String toString() { return "Kiwi"; } } static class LegComparator implements Comparator<Animal> { @Override public int compare(Animal first, Animal second) { if (first.countLeg == second.countLeg) { return 0; } return first.countLeg > second.countLeg ? 1 : -1; } } static class TailComparator implements Comparator<Animal> { @Override public int compare(Animal first, Animal second) { if ((first.tail && second.tail) || !(first.tail && second.tail)) { return 0; } return first.tail ? 1 : -1; } } }