A common task in object oriented programming is comparing two objects for the purpose of sorting. In Java the useful Comparable> Interface exists. I’ve found myself implementing the compareTo method from this Interface plenty of times. But there is something that bothers me everytime: the complexity of the code this implementation creates. Let me explain.
One of my goals is to always write easily readable code. But this is really hard when implementing a compareTo method for an object with more than two fields. Let’s take a look at an example.