Possible Duplicate:
Sorting arraylist in Android in alphabetical order (case insensitive)
Hi everybody I have a small problem in sorting.
I am taking one collection of type String. When I am sorting it is giving me the wrong result that I was expecting
Here is my code :
List <String> caps = new ArrayList<String>();
caps.add("Alpha");
caps.add("Beta");
caps.add("alpha1");
caps.add("Delta");
caps.add("theta");
Collections.sort(caps);
It is sorting like:
Alpha
Beta
Delta
alpha1
theta.
Here it can take any type of string even uppercase / lowercase. But the lowercase words are coming later.
I want the out put like :
Alpha
alpha1
Beta
Delta
theta
Is there an easy built-in method for this?
Collections.sort();lets you pass a custom comparator for ordering. For case insensitive orderingStringclass provides a static final comparator calledCASE_INSENSITIVE_ORDER.So in your case all that’s needed is:
Collections.sort(caps, String.CASE_INSENSITIVE_ORDER);