• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1680
  • Last Modified:

Sorting Java String Array ignoring the case

Hello,
I know the Ararys class give a series of sorting method in java. But I want to sort an array of strings ignoring the case. Do we have a quick way of doing in java within re-inventing the wheel?.

Eg. Given
String names[] = { "Joe", "Arnold", "Pearl", "andy", "jill", peter"};

Result
String names = {"andy", "Arnold", "jill", "Joe", "Pearl", "peter"}

Any ideas.
{Note : I am trying to avoid lengthy ways of doing this. I will evaluate the shortest way of doing this and award the points to that person}.
0
prain
Asked:
prain
1 Solution
 
CEHJCommented:
           List lnames = new ArrayList(Arrays.asList(names));
            Collections.sort(lnames, String.CASE_INSENSITIVE_ORDER);
            System.out.println(lnames);
0
 
Bart CremersJava ArchitectCommented:
       String[] names = { "Joe", "Arnold", "Pearl", "andy", "jill", "peter"};
        Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
        System.out.println("Arrays.toString(names) = " + Arrays.toString(names));
0
 
elfe69Commented:
You can use Arrays.sort():

Arrays.sort(names,String.CASE_INSENSITIVE_ORDER);


Or you can use a comparator, look at the sort() method in the Collection class and the Comparator class for that.
0
 
prainAuthor Commented:
I like Bart_Cr's method.
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now