Java how to sort lowercase before uppercase strings

前端 未结 6 1007
南旧
南旧 2021-01-11 15:56

I want the files to be ordered by their abs path name, but I want the lowercase to be sorted before the uppercase. Example: Let\'s say I got 4 files:

files2.         


        
6条回答
  •  悲&欢浪女
    2021-01-11 16:23

    Try this simple implementation :

    public static void main(String[] args) {
                List list = new ArrayList();
                list.add("a");
                list.add("b");
                list.add("A");
                list.add("B");
                System.out.println(list);
                Collections.sort(list, new Comparator() {
                        @Override
                        public int compare(String o1, String o2) {
                                if (o1.toLowerCase().equals(o2.toLowerCase())) {
                                        if (o1.toLowerCase().equals(o1)) {
                                                return -1;
                                        } else {
                                                return  1;
                                        }
                                } else {
                                        return o1.toLowerCase().compareTo(o2.toLowerCase());
    
                                }
                        }
                });
                System.out.println(list);
        }
    

提交回复
热议问题