Java/Android gets the file list of the folder (file.listFiles()) and sorts by name, Chinese takes precedence

Keywords: Mobile ascii

Sort rule

Because it's Chinese people who are accustomed to putting Chinese folders in front of them, they add their own sorting rules on the basis of other blogs (https://blog.csdn.net/da_caoyuan/article/details/56664673).

The default sorting rule is to sort according to the ASCII code table (http://ascii.911cha.com/), and the steps of sorting are as follows: partial symbol - > number - > partial symbol - > uppercase letter - > partial symbol - > lowercase letter - > partial symbol - > Chinese.

So I modified the logic, and the sorting became: Chinese - > number - > letter (case insensitive) - > special character

Algorithm principle

1. Case insensitive string

2. Change Chinese in the string into the initial of Pinyin

3. Identify the first character of the string. If it is Chinese, add the character "." in front of it. It is used in compareTo method to rank before the number

4. Identify the first character of the string. If it is a symbol (see the ASCII table), add the character "{" before it, which is used to rank after Z in the compareTo method

Code

The code is not annotated, and it is relatively simple

        File[] files = new File(path).listFiles();
        List<File> list;
        if (files == null) {
            list = new ArrayList<>();
        } else {
            list = Arrays.asList(files);
        }
        Collections.sort(list, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                String name1 = o1.getName().toUpperCase();
                char c1 = name1.toCharArray()[0];
                String name11 = "";
                for (char c11 : name1.toCharArray()) {
                    name11 += Pinyin.toPinyin(c11);
                }

                if (c1 >= 0x4E00 && c1 <= 0x9FA5) {
                    name1 = "." + name11;
                } else if (c1 < 48) {
                    name1 = "{" + name11;
                } else if (c1 > 57 && c1 < 65) {
                    name1 = "{" + name11;
                }

                String name2 = o2.getName().toUpperCase();

                char c2 = name2.toCharArray()[0];

                String name22 = "";
                for (char c22 : name2.toCharArray()) {
                    name22 += Pinyin.toPinyin(c22);
                }

                if (c2 >= 0x4E00 && c2 <= 0x9FA5) {
                    name2 = "." + name22;
                } else if (c2 < 48) {
                    name2 = "{" + name22;
                } else if (c2 > 57 && c2 < 65) {
                    name2 = "{" + name22;
                }
                return name1.compareTo(name2);
            }
        });

Posted by parijat_php on Mon, 09 Dec 2019 17:56:40 -0800