Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:59:39+00:00 2026-06-06T09:59:39+00:00

I need to sort list of strings in the alphabetical order: List<String> list =

  • 0

I need to sort list of strings in the alphabetical order:

List<String> list = new ArrayList();
list.add("development");
list.add("Development");
list.add("aa");
list.add("AA");
list.add("Aa");

A common way to do it is to use comparator:

Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

The problem of the CaseInsensitiveComparator that “AA” is equals to “aa”. Strings appear in the result according to the order of adding for the same values, and it is not correct:

"aa","AA","Aa","development","Development"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:59:42+00:00Added an answer on June 6, 2026 at 9:59 am

    If you don’t want to add a dependency on Guava (per Michael’s answer) then this comparator is equivalent:

    private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
        public int compare(String str1, String str2) {
            int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
            if (res == 0) {
                res = str1.compareTo(str2);
            }
            return res;
        }
    };
    
    Collections.sort(list, ALPHABETICAL_ORDER);
    

    And I think it is just as easy to understand and code …

    The last 4 lines of the method can written more concisely as follows:

            return (res != 0) ? res : str1.compareTo(str2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of strings. Each string follows the pattern {Path}\UpdateTo{Version}-{Order}. I need
I have List[(String,String)] and I need to sort them by the 2nd value and
Let me explain: I need to sort a list in ascending order, while results
I have the following List : List<Dictionary<int, Dictionary<string, string>>> lngList lngList.Add(new Dictionary<int,Dictionary<string,string>>().Add(1,new Dictionary<string,string>().Add(Item1Key,Item1Value))); lngList.Add(new
I need to sort a List based on the difference between the strings in
I need to add string values to a string List and every added string
My need is to sort the ArrayList of Strings, based on specific index range.
I have a list of items(i.e Strings) which I need to sort/filter. The end
I get a list of items from my Repository. Now I need to sort
I need to sort string, and I came up with the following function. def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.