Java how to sort lowercase before uppercase strings

前端 未结 6 1012
南旧
南旧 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:24

    As suggested by others, Collator does what you want. Writing one of those collator rules looked a bit scary, but it looks like the standard English Collator does exactly what you want:

    public static void main(String... args)
    {
        List items = Arrays.asList("b", "A", "a", "B");
        Collections.sort(items, Collator.getInstance(Locale.ENGLISH));
        System.out.println(items);
    }
    

    gives:

    [a, A, b, B]
    

提交回复
热议问题