Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to sort strings in the (case sensitive) alphabetical order

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" 
like image 251
Michael Avatar asked Jun 24 '12 09:06

Michael


People also ask

How do you sort a string in alphabetical order?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.

How do you sort a string case-insensitive?

An array can be sorted in case-insensitive order using the java. util. Arrays. sort() method.

How do you alphabetically sort strings in Python?

Summary. Use the Python List sort() method to sort a list in place. The sort() method sorts the string elements in alphabetical order and sorts the numeric elements from smallest to largest. Use the sort(reverse=True) to reverse the default sort order.


2 Answers

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); 
like image 131
Stephen C Avatar answered Sep 21 '22 11:09

Stephen C


The simple way to solve the problem is to use ComparisonChain from Guava http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ComparisonChain.html

private static Comparator<String> stringAlphabeticalComparator = new Comparator<String>() {         public int compare(String str1, String str2) {             return ComparisonChain.start().                                 compare(str1,str2, String.CASE_INSENSITIVE_ORDER).                                 compare(str1,str2).                                 result();          }  }; Collections.sort(list, stringAlphabeticalComparator); 

The first comparator from the chain will sort strings according to the case insensitive order, and the second comparator will sort strings according to the case insensitive order. As excepted strings appear in the result according to the alphabetical order:

"AA","Aa","aa","Development","development" 
like image 25
Michael Avatar answered Sep 21 '22 11:09

Michael