How to sort alphabetically while ignoring case sensitive?
Collections.sort(listToSort, String.CASE_INSENSITIVE_ORDER);
It is very unclear what you are trying to do, but you can sort a list like this:
List<String> fruits = new ArrayList<String>(7);
fruits.add("Pineapple");
fruits.add("apple");
fruits.add("apricot");
fruits.add("Banana");
fruits.add("mango");
fruits.add("melon");
fruits.add("peach");
System.out.println("Unsorted: " + fruits);
Collections.sort(fruits, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
});
System.out.println("Sorted: " + fruits);
Collections.sort()
lets you pass a custom comparator for ordering. For case insensitive ordering String
class provides a static final comparator called CASE_INSENSITIVE_ORDER
.
So in your case all that's needed is:
Collections.sort(caps, String.CASE_INSENSITIVE_ORDER);