根据文件或文件夹对文件进行排序

时间:2011-11-23 14:00:38

标签: android

我有一个包含多个文件和文件夹的名称数组...现在我想根据文件和文件夹排序名称。首先是所有文件夹然后显示所有文件。我有变量来检查是否特定数组的索引是文件或文件夹。但无法想到逻辑.. 我附上了我的一些代码。

//////////////////////////////// case 0://按名称排序                         {

                        if(m_sortType == SORT_BY_NAME && temp==false)
                        {
                            m_sortType = SORT_BY_NAME;
                            m_sortOrder=SORT_ORDER_DESCENDING;
                            temp= true;
                            //Log.d("SORTING", "SORT - NAME - DES");
                        }
                        else
                        {
                            m_sortType = SORT_BY_NAME;
                            m_sortOrder=SORT_ORDER_ASCENDING;
                            temp=false;
                            //Log.d("SORTING", "SORT - NAME - AES");
                        }

                        //Log.d("SORTING", "Data bfore sort");
                        for (int k=0; k<m_adapter.m_env.m_count; k++)
                            //Log.d("SORTING DATA", k + ": " + m_adapter.m_env.m_fs.get(m_SortArray[k]).m_name);

                        m_adapter.sortListing(m_sortType,m_sortOrder);

                        //Log.d("SORTING", "Data after sort");
                        for (int k=0; k<m_adapter.m_env.m_count; k++)
                            //Log.d("SORTING DATA", k + ": " + m_adapter.m_env.m_fs.get(m_SortArray[k]).m_name);

                        //Refresh();
                        break;
                    }

//////////////// private void sortListing(int sortType,int sortOrder)         {             m_sortType = sortType;             m_sortOrder = sortOrder;             Arrays.sort(m_SortArray,new Comparator(){

            public int compare(Integer a1, Integer a2)
            {

                if(m_sortType == SORT_BY_NAME)
                {
                    String s1 = null,s2 = null;
                    FileFolderEnum t2 = null;
                    FileFolderEnum t1 = null;
                    int i;
                    if(m_sortOrder==SORT_ORDER_ASCENDING)
                    {
                        s1 = m_env.m_fs.get(a1).m_name;
                        s2 = m_env.m_fs.get(a2).m_name;
                        t1 = m_env.m_fs.get(a1).m_type;
                        t2 = m_env.m_fs.get(a2).m_type;
                    }
                    else if(m_sortOrder==SORT_ORDER_DESCENDING)
                    {
                        s1 = m_env.m_fs.get(a2).m_name;
                        s2 = m_env.m_fs.get(a1).m_name;
                        t1 = m_env.m_fs.get(a2).m_type;
                        t2 = m_env.m_fs.get(a1).m_type;
                    }
                    //Log.d("SORTING COMPARE", "(" + Integer.toString(a1)+") s1: " + s1);
                    //Log.d("SORTING COMPARE", "(" + Integer.toString(a2)+") s2: " + s2);
                    if((t1.equals(CFileFolder.FileFolderEnum.FFE_FOLDER)&&(t2.equals(CFileFolder.FileFolderEnum.FFE_FOLDER))))
                    {
                         i=s1.compareToIgnoreCase (s2);
                    }
                     i=s1.compareToIgnoreCase (s2);

                    //Log.d("SORTING COMPARE", "s1.compareTo(s2): " + Integer.toString(i));
                    return i;
                }

///////////////////

3 个答案:

答案 0 :(得分:14)

我从我自己的文件浏览器中删除了代码。根据需要使用。 :

File[] directoryList = currentFolder.listFiles(); 
if (directoryList != null) {
   List<File> directoryListing = new ArrayList<File>();
   directoryListing.addAll(Arrays.asList(directoryList));
   Collections.sort(directoryListing, new SortFileName());
   Collections.sort(directoryListing, new SortFolder());
}


//sorts based on the files name
public class SortFileName implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
          return f1.getName().compareTo(f2.getName());
    }
}

//sorts based on a file or folder. folders will be listed first
public class SortFolder implements Comparator<File> {
    @Override
    public int compare(File f1, File f2) {
         if (f1.isDirectory() == f2.isDirectory())
            return 0;
         else if (f1.isDirectory() && !f2.isDirectory())
            return -1;
         else
            return 1;
          }
}

答案 1 :(得分:5)

我知道这是一个很老的帖子,但我需要解决这个问题并且发现了帖子。 dymmeh的解决方案是一个良好的开端,但我想在一次通过排序,我想要类似Windows的排序(不考虑案例)。这就是我想出的:

import java.io.File;
import java.util.Comparator;

public class FileComparator implements Comparator<File> {
    @Override
    public int compare(File lhs, File rhs) {

        if (lhs.isDirectory() == rhs.isDirectory()) { // Both files are directory OR file, compare by name
            return lhs.getName().toLowerCase().compareTo(rhs.getName().toLowerCase());
        } else if (lhs.isDirectory()) { // Directories before files
            return -1;
        } else { // rhs must be a directory
            return 1;
        }
    }
}

用法很简单:

final File file = new File(_directory);
final File[] files = file.listFiles();
Arrays.sort(files, new FileComparator());

答案 2 :(得分:0)

你有变量来检查名称是文件和文件夹,我可以想办法根据你的要求对它们进行排序。制作两个ArrayLists。将这两个ArrayList中的文件夹和文件分开,然后根据名称对字符串进行简单的排序操作。然后只需在文件夹列表的末尾附加文件列表。可能会起作用。看似简单。