This article shares the specific code for Android folder sorting for your reference. The specific content is as follows
Based on name:
/** * Sort by file name * @param filePath */ public static ArrayList<String> orderByName(String filePath) { ArrayList<String> FileNameList = new ArrayList<String>(); File file = new File(filePath); File[] files = (); List fileList = (files); (fileList, new Comparator<File>() { @Override public int compare(File o1, File o2) { if (() && ()) return -1; if (() && ()) return 1; return ().compareTo(()); } }); for (File file1 : files) { if (()) { (()); } } return FileNameList; }
Based on the latest modification time:
/** * Sort by file modification time * @param filePath */ public static ArrayList<String> orderByDate(String filePath) { ArrayList<String> FileNameList = new ArrayList<String>(); File file = new File(filePath); File[] files = (); (files, new Comparator<File>() { public int compare(File f1, File f2) { long diff = () - (); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;// If the if is modified to return -1 and the order will be decremented } public boolean equals(Object obj) { return true; } }); for (File file1 : files) { if (()) { (()); } } return FileNameList; }
Based on size:
/** * Sort by file size * @param filePath */ public static ArrayList<String> orderBySize(String filePath) { ArrayList<String> FileNameList = new ArrayList<String>(); File file = new File(filePath); File[] files = (); List<File> fileList = (files); (fileList, new Comparator<File>() { public int compare(File f1, File f2) { long s1 = getFolderSize(f1); long s2 = getFolderSize(f2); long diff = s1 - s2; if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;// If the if is modified to return -1 and the order will be decremented } public boolean equals(Object obj) { return true; } }); for (File file1 : files) { if (()) { (()); } } return FileNameList; } /** * Get folder size * @param file File instance * @return long */ public static long getFolderSize(File file) { long size = 0; try { [] fileList = (); for (int i = 0; i < ; i++) { if (fileList[i].isDirectory()) { size = size + getFolderSize(fileList[i]); } else { size = size + fileList[i].length(); } } } catch (Exception e) { (); } return size; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.