java - 如何对文件和目录列表进行排序,以便首先列出目录

标签 java android file

我有一个文件和目录列表,但列表中没有组织。我想用首先按字母顺序列出的目录对它们进行排序,然后是文件。我怎样才能做到这一点?

private void fill(File[] files) {
        this.directoryEntries.clear();

        // and the ".." == 'Up one level'
        if(this.currentDirectory.getParent() != null && !this.currentDirectory.equals("/sd card"))
                this.directoryEntries.add(new IconifiedText(
                                getString(R.string.up_one_level),
                                getResources().getDrawable(R.drawable.uponelevel)));

        Drawable currentIcon = null;
        for (File current_File : files){
                if (current_File.isDirectory()) {
                        currentIcon = getResources().getDrawable(R.drawable.folder);
                }else{
                        String fileName = current_File.getName();
                        /* Determine the Icon to be used,
                         * depending on the FileEndings defined in:
                         * res/values/fileendings.xml. */
                        if(checkEndsWithInStringArray(fileName, getResources().
                                getStringArray(R.array.fileEndingJs))){
                                currentIcon = getResources().getDrawable(R.drawable.mimejs);
                        }else if(checkEndsWithInStringArray(fileName, getResources().
                                getStringArray(R.array.fileEndingHTML))){
                                currentIcon = getResources().getDrawable(R.drawable.mimehtml);
                        }else if(checkEndsWithInStringArray(fileName, getResources().
                                getStringArray(R.array.fileEndingCSS))){
                                currentIcon = getResources().getDrawable(R.drawable.mimecss);
                        }else if(checkEndsWithInStringArray(fileName, getResources().
                                getStringArray(R.array.fileEndingXML))){
                                currentIcon = getResources().getDrawable(R.drawable.mimexml);
                        }else if(checkEndsWithInStringArray(fileName, getResources().
                                getStringArray(R.array.fileEndingPhp))){
                            currentIcon = getResources().getDrawable(R.drawable.mimephp);
                    }else{
                            currentIcon = getResources().getDrawable(R.drawable.mimetxt);
                        }                              
                }
                switch (this.displayMode) {
                        case ABSOLUTE:
                                /* On absolute Mode, we show the full path */
                                this.directoryEntries.add(new IconifiedText(current_File
                                                .getPath(), currentIcon));
                                break;
                        case RELATIVE:
                                /* On relative Mode, we have to cut the
                                 * current-path at the beginning */
                                int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length();
                                this.directoryEntries.add(new IconifiedText(
                                                current_File.getAbsolutePath().
                                                substring(currentPathStringLenght),
                                                currentIcon));

                                break;
                }
        }
        Collections.sort(this.directoryEntries);

        itla.setListItems(this.directoryEntries);              
        this.setListAdapter(itla);
}

最佳答案

写一个ComparatorisDirectory()

排序
class FileTypeComparator implements Comparator<File> {

    @Override
    public int compare(File file1, File file2) {

        if (file1.isDirectory() && file2.isFile())
            return -1;
        if (file1.isDirectory() && file2.isDirectory()) {
            return 0;
        }
        if (file1.isFile() && file2.isFile()) {
            return 0;
        }
        return 1;
    }
}

还有一个按getName()排序的

class FileNameComparator implements Comparator<File> {

    @Override
    public int compare(File file1, File file2) {

        return String.CASE_INSENSITIVE_ORDER.compare(file1.getName(),
                file2.getName());
    }
}

然后使用 Guavas Ordering.compound(Comparator comparator2)结合两个 Comparators

Collections.sort(files,Ordering.from(new FileTypeComparator()).compound(new FileNameComparator()));

关于java - 如何对文件和目录列表进行排序,以便首先列出目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15672195/

相关文章:

android - Environment.getExternalStorageDirectory() 方法的 Android 文档不完整

c - 如何创建保存从 0000 到 1000 的银行帐号的随机访问文件

node.js - 招摇: is '😱 Could not render this component, see the console' an error message or can it be ignored

java - 如何为ANTLR指定目标包?

android - 在 OpenGL ES 中渲染期间编译着色器是一种好的/有效的做法吗?

java - 包含泛型成员的类数组的问题

android - 错误处理购买 [BM-CPH-08]

string - 比较行并从匹配中创建新行

java - 如何从具有重复条目的过滤列表中删除特定索引?

java - 使用jsoup提取文本的某些部分