### Question:
StringComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if ( o1 == o2 ) { return 0; } else if ( o1 == null ) { return -1; } else if ( o2 == null ) { return 1; } else if ( o1.length() < o2.length() ) { return -1; } else if ( o1.length() > o2.length() ) { return 1; } else { return o1.compareTo(o2); } } private  StringComparator(); static final StringComparator getInstance(); @Override int compare(String o1, String o2);  }### Answer:
@Test public void testCompare() { ArrayList<String> rows = new ArrayList<String>(20); for ( int i=99; i>=0; i-- ) { rows.add("row"+i); } ArrayList<String> expects = new ArrayList<String>(20); for ( int i=0; i<100; i++ ) { expects.add("row"+i); } Collections.sort(rows, StringComparator.getInstance()); assertArrayEquals(expects.toArray(), rows.toArray()); }