直接上代码:
首先做个比较器
public Comparator<File> compareByName;
然后就是比较的主要方法
compareByName = new Comparator<File>() {
public int compare(String arg0, String arg1) {
arg1=arg1.replaceAll("-","");//过滤掉文件里的这个符号 arg0=arg0.replaceAll("-","");arg1 = arg1.replaceAll("一", "11");//这个是让中文数字变成可比较的值
arg0 = arg0.replaceAll("一", "11"));
arg0 = arg0.replaceAll("二", "22");
arg0 = arg0.replaceAll("三", "33"); arg0 = arg0.replaceAll("四", "44"); arg0 = arg0.replaceAll("五", "55"); arg0 = arg0.replaceAll("六", "66"); arg0 = arg0.replaceAll("七", "77"); arg0 = arg0.replaceAll("八", "88"); arg0 = arg0.replaceAll("九", "99");arg1 = arg1.replace("一", "11");
arg1 = arg1.replaceAll("二", "22");
arg1 = arg1.replaceAll("三", "33"); arg1 = arg1.replaceAll("四", "44"); arg1 = arg1.replaceAll("五", "55"); arg1 = arg1.replaceAll("六", "66"); arg1 = arg1.replaceAll("七", "77"); arg1 = arg1.replaceAll("八", "88"); arg1 = arg1.replaceAll("九", "99");if (arg0.compareTo(arg1) > 0) {
return 1;
} else if (arg0.compareTo(arg1) < 0) {return -1;
} else {return 0;
} }public int compare(File arg0, File arg1) {
//将FILE文件 变成STRING再去比较
return compare(arg0.getName(), arg1.getName()); // TODO Auto-generated method stub}
};
最后 Collections.sort(文件集合LIST, compareByName);
LIST就是已经排序序的了