Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly sort upper and lowercase Strings in Array in Android? [duplicate]

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?

like image 798
Ethan Allen Avatar asked Dec 08 '22 19:12

Ethan Allen


1 Answers

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);

like image 88
jlordo Avatar answered May 04 '23 01:05

jlordo