Array List of String Sorting Method

后端 未结 6 518
小蘑菇
小蘑菇 2021-01-12 10:39

i have an Array List with Following values

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

when i used

Collections.s         


        
6条回答
  •  一生所求
    2021-01-12 11:06

    You can do with custom Comparator.

    Try this:

        // list containing String objects
        List list = new ArrayList<>();
    
        // call sort() with list and Comparator which
        // compares String objects ignoring case
        Collections.sort(list, new Comparator(){
            @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.

提交回复
热议问题