Keeping upper and lowercase letters together : Collections Sort « Collections « Java Tutorial






import java.util.Arrays;
import java.util.Comparator;

public class MainClass {
  public static void main(String[] args) {
    String[] sa = new String[] { "adf", "fdsa", "ASDF", "FSA", "r", "R" };

    System.out.println("Before sorting: " + Arrays.asList(sa));
    Arrays.sort(sa, new AlphabeticComparator());
    System.out.println("After sorting: " + Arrays.asList(sa));
  }
}

class AlphabeticComparator implements Comparator {
  public int compare(Object o1, Object o2) {
    String s1 = (String) o1;
    String s2 = (String) o2;
    return s1.toLowerCase().compareTo(s2.toLowerCase());
  }
}
/**/
Before sorting: [adf, fdsa, ASDF, FSA, r, R]
After sorting: [adf, ASDF, fdsa, FSA, r, R]








9.42.Collections Sort
9.42.1.Sorting a List
9.42.2.Reversing Order
9.42.3.Sorting a Collection containing user defined Objects
9.42.4.Keeping upper and lowercase letters together