If your program ever used toString() for lexical sorting, using the natural order so that the execution of the program depends on it, then it would be advisable to override the default value of the toString() class, which is extended. In this case, you must make the toString() method final and clearly document that it is used for ordering.
However, it would be much better to create another method that returns String , and create an order based on this result, possibly creating a specific Comparator for sorting. See For example, the last name() method used for enumerations in Java. In general, this creates the same string, toString() , but it is still possible to execute an order with it, even if toString() was canceled.
If you use the latter method, then Ordering.usingToString() will not be very useful.
source share