What will happen if I supply a non-transitive Comparator to Collections.sort? Can I run into infinite loop?
A small test I wrote produced an output, but I want to make sure this will always be the case.
The problem is that in some cases, my comparator can produce cycles, and in this case I just want to make sure it will not run into infinite loop. I don’t care about the actual result.
The Java docs say that you must make sure that your comparator is transitive. If you supply a comparator that doesn’t adhere to what was requested, all bets are off. It might work for a given implementation but might crash horribly (
std::sortin C++ does) in another.In short, you shouldn’t rely on it working even if it does for some or other examples.