operator< comparing multiple fields

I’d like to do it all by myself.. You should only compare the values of Obj::field2 if the values of Obj::field1 are equal. The easy-to-understand way: /* This will meet the requirements of Strict-Weak-Ordering */ if (a.field1 != b.field1) return a.field1 < b.field1; else return a.field2 < b.field2; The correct (recommended) way: The “correct” way … Read more

String Comparison in Java

Leading from answers from @Bozho and @aioobe, lexicographic comparisons are similar to the ordering that one might find in a dictionary. The Java String class provides the .compareTo () method in order to lexicographically compare Strings. It is used like this “apple”.compareTo (“banana”). The return of this method is an int which can be interpreted … Read more