Sorting Java String Array ignoring the case

Posted on 2007-10-08
Last Modified: 2013-12-29
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"};

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}.
Question by:prain
    LVL 86

    Expert Comment

               List lnames = new ArrayList(Arrays.asList(names));
                Collections.sort(lnames, String.CASE_INSENSITIVE_ORDER);
    LVL 13

    Accepted Solution

           String[] names = { "Joe", "Arnold", "Pearl", "andy", "jill", "peter"};
            Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
            System.out.println("Arrays.toString(names) = " + Arrays.toString(names));
    LVL 11

    Expert Comment

    You can use Arrays.sort():


    Or you can use a comparator, look at the sort() method in the Collection class and the Comparator class for that.

    Author Comment

    I like Bart_Cr's method.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Title # Comments Views Activity
    nested class vs inner class 5 36
    Fix45 challenge 15 65
    ArrayLists in ArrayLists/HashMaps? 4 26
    create a gui in perl 3 20
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now