/* StringSorterLenLexico.java ** An instance of this class has methods (inherited from its parent) ** by which to sort an array (or a segment thereof) into ascending order ** using the classic Selection Sort algorithm. What this class provides ** is a method that implements the "Length-Lexicographic" ordering relation ** upon Strings, which says that shorter strings precede longer strings and, ** among strings of the same length, they are ordered lexicographically. */ public class StringSorterLenLexico extends Sorter { /* Returns true iff s1 < s2 according to Length-Lexicographic ** ordering, which is to say that either ** (1) s1.length() < s2.length() or ** (2) s1.length() = s2.length and s1.compareTo(s2) < 0 */ protected boolean isLessThan(String s1, String s2) { final int lengthDiff = s1.length() - s2.length(); if (lengthDiff != 0) { return lengthDiff < 0; // returns true if s1 is shorter than s2, } // false if s1 is longer than s2 else { return s1.compareTo(s2) < 0; // s1 and s2 are of same length, } // so revert to lexicographic order }