/* StringSorterLenLexico.java ** An instance of this class has methods (inherited from its parent) ** by which to sort an array (or a segment thereof) of Strings into ** ascending order. It supplies the body of isLessThan() for the ** purpose of defining/implementing an ordering on strings. Here, ** that is "length-lexicographic" ordering, which says that string ** s1 is less than string s2 iff either ** (1) s1 is shorter than s2 (in length), or ** (2) s1 and s2 are of the same length and s1 is less than s2 based ** on lexicographic ordering. */ public class StringSorterLenLexico extends StringSorter { /* 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 */ @Override protected boolean isLessThan(String s1, String s2) { final int lengthDiff = s1.length() - s2.length(); if (lengthDiff < 0) { return true; } // s1 is shorter than s2, so s1 < s2 else if (lengthDiff > 0) { return false; } // s1 is longer than s2, so s1 > s2 else { return s1.compareTo(s2) < 0; } // s1 and s2 are of same length, so } // revert to lexicographic order }