Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array List of String Sorting Method

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

like image 461
Prabu Avatar asked Oct 15 '13 13:10

Prabu


2 Answers

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]
like image 99
Ulaga Avatar answered Sep 22 '22 11:09

Ulaga


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);
        }
});
like image 33
Prabhakaran Ramaswamy Avatar answered Sep 26 '22 11:09

Prabhakaran Ramaswamy