2013-03-28 57 views
1

我有一個文件和目錄的列表,但沒有組織列表。我想按字母順序排列目錄,然後是文件。我怎樣才能做到這一點?如何排序文件和目錄的列表,以便首先列出目錄

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); 
} 
+0

製作您自己的比較器,首先檢查它是否是目錄,然後比較文件名。 – 2013-03-28 00:37:10

+0

啊謝謝!在比較器上找到一些好東西來閱讀:) – RapsFan1981 2013-03-28 01:06:44

回答

4

Comparator的排序由isDirectory()

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

然後用番石榴Ordering.compound(Comparator comparator2)兩個Comparators

結合
相關問題