7

i have an Array List with Following values

ArrayList [Admin,Readonly,CSR,adminuser,user,customer]

when i used

Collections.sort(ArrayList)

i'm getting the Following Result

[Admin,CSR,Readonly,adminuser,customer,user]

as per the Java doc the above results are correct, but my Expectation is (sorting irrespective of case (upper / lower case)

[Admin,adminuser,CSR,customer,Readonly,user]

provide an help how will do the sorting irrespective of case in java, is there any other method available

Note: i will do an Automate test for checking the sorting order in the Web table

regards

prabu

olyv
  • 3,699
  • 5
  • 37
  • 67
Prabu
  • 3,550
  • 9
  • 44
  • 85
  • take a look at java [Comparator](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html) – Eugen Halca Oct 15 '13 at 13:32
  • Have you tried this : http://stackoverflow.com/questions/7469643/how-to-sort-alphabetically-while-ignoring-case-sensitive – realUser404 Oct 15 '13 at 13:33
  • Just add a comparator to Collections.sort() which will ignore the case. You could write your own, but there is one already defined in String class: String.CASE_INSENSITIVE_ORDER. So you sort call will be "Collections.sort(your_List, String.CASE_INSENSITIVE_ORDER)". The full solution is described by Ulaga below. – mins May 25 '14 at 18:39

6 Answers6

7

This'll do,

Collections.sort(yourList, String.CASE_INSENSITIVE_ORDER);

this is i have tried,

ArrayList<String> myList=new ArrayList<String>();
Collections.addAll(myList,"Admin","Readonly","CSR","adminuser","user","customer");
System.out.println(myList);
Collections.sort(myList, String.CASE_INSENSITIVE_ORDER);
System.out.println(myList);

the following output i got,

[Admin, Readonly, CSR, adminuser, user, customer]
[Admin, adminuser, CSR, customer, Readonly, user]
Ulaga
  • 863
  • 1
  • 8
  • 17
  • Collections.sort(before_fn, String.CASE_INSENSITIVE_ORDER); for (String temp : before_fn) { System.out.println("Ascending Order" + temp); } its working correctly, let me know the Reversse Order for that same
    when i tried Collections.sort(before_fn, Collections.reverseOrder()); its working i required irrespective of case
    – Prabu Oct 16 '13 at 04:26
4

You can use your own comparator like this to sort irrespective of case (upper / lower case)

Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2)
        {    
            return  s1.compareToIgnoreCase(s2);
        }
});
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
2

You can do with custom Comparator.

Try this:

    // list containing String objects
    List<String> list = new ArrayList<>();

    // call sort() with list and Comparator which
    // compares String objects ignoring case
    Collections.sort(list, new Comparator<String>(){
        @Override
        public int compare(String o1, String o2) {
            return o1.compareToIgnoreCase(o2);
        }
    });

You will need to pass Comparator instance in Collections.sort() method which compares String objects ignoring case.

nullptr
  • 3,320
  • 7
  • 35
  • 68
1
public class SortIgnoreCase implements Comparator<Object> {
    public int compare(Object o1, Object o2) {
        String s1 = (String) o1;
        String s2 = (String) o2;
        return s1.toLowerCase().compareTo(s2.toLowerCase());
    }
}

then

Collections.sort(ArrayList, new SortIgnoreCase());
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
  • Not sure why you would do Comparator instead of Comparator when you explicitly typecase them immediately after entering the method. – Jazzy Josh Oct 15 '13 at 13:45
1
Collections.sort(ArrayList, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return s1.toLowerCase().compareTo(s2.toLowerCase());
        }
    });
gjh
  • 11
  • 1
0

Answer is simple - big letters have lower number in ASCII. So default comparing works fine.

Eel Lee
  • 3,513
  • 2
  • 31
  • 49