From 9d56eec131c38937048a985325b9e7b433ee1b21 Mon Sep 17 00:00:00 2001 From: muqing153 <103228194+muqing153@users.noreply.github.com> Date: Tue, 31 Jan 2023 16:26:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E4=BA=86=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=8E=92=E5=BA=8F=E5=8A=9F=E8=83=BD,?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=BA=86=E6=96=87=E4=BB=B6=E5=A4=B9=E5=9C=A8?= =?UTF-8?q?=E5=89=8D=E9=9D=A2=E6=96=87=E4=BB=B6=E5=9C=A8=E5=90=8E=E9=9D=A2?= =?UTF-8?q?,=E6=B7=BB=E5=8A=A0=E6=99=BA=E8=83=BD=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/coldmint/rust/pro/FileSort.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 app/src/main/java/com/coldmint/rust/pro/FileSort.java diff --git a/app/src/main/java/com/coldmint/rust/pro/FileSort.java b/app/src/main/java/com/coldmint/rust/pro/FileSort.java new file mode 100644 index 0000000..563a1c9 --- /dev/null +++ b/app/src/main/java/com/coldmint/rust/pro/FileSort.java @@ -0,0 +1,131 @@ +import java.io.File; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class FileSort { + final int name=0,time=1,size=2,type=3; + public static String[] type_list; + public FileSort(List a,int manner){ + switch (manner){ + case name: + sort_name(a); + break; + case time: + sort_time(a); + break; + case size: + sort_size(a); + break; + case type: + sort_type(a); + break; + default: + } + } + public static void sort_front(List a){ + Collections.sort(a, new Comparator() { + @Override + public int compare(File o1, File o2) { + if (o1.isDirectory() && o2.isFile()) + return -1; + if (o1.isFile() && o2.isDirectory()) + return 1; + return 0; + } + }); + } + + public static void sort_name(List a){ + Collections.sort(a,new Comparator() { + @Override + public int compare(File o1, File o2) { + if (o1.isDirectory() && o2.isFile()) + return -1; + if (o1.isFile() && o2.isDirectory()) + return 1; + return o1.getName().compareTo(o2.getName()); + } + }); + sort_front(a); + } + public static void sort_time(List a){ + Collections.sort(a,new Comparator() { + @Override + public int compare(File f1, File f2) { + long diff = f1.lastModified() - f2.lastModified(); + if (diff > 0) + return 1; + else if (diff == 0) + return 0; + else + return -1; + //如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减 + } + }); + sort_front(a); + } + + public void sort_size(List a){ + + Collections.sort(a, new Comparator() { + @Override + public int compare(File o1, File o2) { + long diff = o1.length() - o2.length(); + if (diff > 0) + return 1; + else if (diff == 0) + return 0; + else + return -1; + } + }); + sort_front(a); + } + public static void sort_type(List a){ + //文件类型排序 按照后缀优先级 +// System.out.println(type_list[0]); + Collections.sort(a, new Comparator() { + @Override + public int compare(File o1, File o2) { + if (o1.isFile()) { + String name_a = o1.getName(); + String o_z = name_a.substring(name_a.lastIndexOf(".")); +// System.out.println(o_z); + //获取 name_a文件名字的后缀 + int length=length(type_list,o_z); + System.out.println(length); + //查找后缀是不是存在于 type_list数据中 如果存在返回存在位置 如果不存在则返回<0的值 + if (length>=0) + return -1; + } else if (o2.isFile()) { + String name_a = o2.getName(); +// System.out.println(name_a); + String o_z = name_a.substring(name_a.lastIndexOf(".")); + + //获取 name_a文件名字的后缀 + int length=length(type_list,o_z); + if (length>=0) + return 1; + } + return 0; + } + }); + } + private static int length(String a){ + for (int i=0;i