java按照文件名排序,中文按拼音字母排序
import java.io.File; import java.text.Collator; import java.util.ArrayList; import java.util.Comparator; public class FileToLook { public static ArrayList<String> list=new ArrayList<String>(); public static void main(String[] args) { File file=new File("F:\\music\\Westlife"); printAll(file); paixu(); } public static void printAll(File file){ if(file.isDirectory()){ for(File f:file.listFiles()){ printAll(f); } }else{ list.add(file.getName()+"-->"+file.length()); } } public static void paixu() { for(int i=0;i<list.size()-1;i++) { for(int j=1;j<list.size()-i;j++) { String a; if(compairByName(list.get(j-1),list.get(j))>0) { a=list.get(j-1); list.set((j-1),list.get(j)); list.set(j,a); } } } for(String s:list) { System.out.println(s); } } public static int compairByName(String file1,String file2){ Comparator<Object> com=Collator.getInstance(java.util.Locale.CHINA); return com.compare(file1, file2); } }