English 中文(简体)
根据档案或文件夹处理档案
原标题:Sorting of files according to file or folder
  • 时间:2011-11-23 14:00:37
  •  标签:
  • android

i have an array of names which have multiple files and folders...now i want to sort the names according to files and folders.all folders first and then all files should display.i have the variable to check whether on particular index of array their is file or folder.but unable to think the logic.. i am attaching some of my codes.

//////////////////////// case 0://Sort By Name {

                        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;
                }

///////////

最佳回答

Code I pulled from my own file browser. Use as you wish. :

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;
          }
}
问题回答

我知道这是一个老的岗位,但我需要解决这个问题,并走到这个岗位上。 乳房的溶解是一个良好的开端,但我希望在一次通行证中进行分类,我希望像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;
        }
    }
}

Usage is straight forward:

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

Well you have the variable to check the name is of file and folder, I can think of a way to sort them as per your requirement. Make two ArrayLists. Separate folders and files in these two ArrayList and then sort both according to the names, simple sort operation for string. Then simply append the files List at the end of Folder List. May be it will work. Seems simple.





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签