Explorar o código

sort by files' name when listFiles

sunyj %!s(int64=8) %!d(string=hai) anos
pai
achega
ba932a3902

+ 27 - 0
report-common/src/main/java/com/uas/report/util/CharUtils.java

@@ -0,0 +1,27 @@
+package com.uas.report.util;
+
+public class CharUtils {
+
+	public static boolean isNumber(char c) {
+		return c >= 48 && c <= 57;
+	}
+
+	public static boolean isCharacter(char c) {
+		return isUpperCharacter(c) || isLowerCharacter(c);
+	}
+
+	public static boolean isUpperCharacter(char c) {
+		return c >= 65 && c <= 90;
+	}
+
+	public static boolean isLowerCharacter(char c) {
+		return c >= 97 && c <= 122;
+	}
+
+	public static char toUpper(char c) {
+		if (isLowerCharacter(c)) {
+			return (char) (c - 32);
+		}
+		return c;
+	}
+}

+ 95 - 12
report/src/main/java/com/uas/report/service/impl/FileServiceImpl.java

@@ -436,22 +436,105 @@ public class FileServiceImpl implements FileService {
 			result.add(getFileInformation(file, isAbsolutePath));
 		} else {
 			File[] files = file.listFiles(fileFilter);
-			// 文件夹放在前面展示
-			List<File> directoryList = new ArrayList<>();
-			List<File> fileList = new ArrayList<>();
-			for (File f : files) {
-				if (f.isDirectory()) {
-					directoryList.add(f);
-				} else {
-					fileList.add(f);
-				}
-			}
-			result.addAll(getFileInformations(directoryList, isAbsolutePath));
-			result.addAll(getFileInformations(fileList, isAbsolutePath));
+			if(!ArrayUtils.isEmpty(files)){
+			    sort(files);
+                // 文件夹放在前面展示
+                List<File> directoryList = new ArrayList<>();
+                List<File> fileList = new ArrayList<>();
+                for (File f : files) {
+                    if (f.isDirectory()) {
+                        directoryList.add(f);
+                    } else {
+                        fileList.add(f);
+                    }
+                }
+                result.addAll(getFileInformations(directoryList, isAbsolutePath));
+                result.addAll(getFileInformations(fileList, isAbsolutePath));
+            }
 		}
 		return result;
 	}
 
+    /**
+     * 根据文件的名称进行排序(增序)
+     *
+     * @param files
+     */
+    public void sort(File[] files) {
+        int N = files.length;
+        int h = 1;
+        while (h < N / 3) {
+            h = h * 3 + 1;
+        }
+        while (h >= 1) {
+            for (int i = h; i < N; i++) {
+                for (int j = i; j >= h && less(files[j], files[j - h]); j -= h) {
+                    exchange(files, j, j - h);
+                }
+            }
+            h /= 3;
+        }
+    }
+
+    private boolean less(File f1, File f2) {
+        char[] array1 = f1.getName().toLowerCase().toCharArray();
+        char[] array2 = f2.getName().toLowerCase().toCharArray();
+        int length1 = array1.length;
+        int length2 = array2.length;
+        // 取最小的长度
+        int length = length1 < length2 ? length1 : length2;
+        for (int i = 0; i < length; i++) {
+            char c1 = array1[i];
+            char c2 = array2[i];
+            // 字母 > 数字 > 其他字符(优先级视为相等)
+            if (CharUtils.isCharacter(c1)) {
+                if (CharUtils.isCharacter(c2)) {
+                    // 均为字母,值越小优先级越高
+                    if (c1 < c2) {
+                        return true;
+                    } else if (c1 > c2) {
+                        return false;
+                    }
+                } else {
+                    return true;
+                }
+            } else if (CharUtils.isNumber(c1)) {
+                if (CharUtils.isCharacter(c2)) {
+                    return false;
+                } else if (CharUtils.isNumber(c2)) {
+                    // 均为数字,数字越小优先级越高
+                    if (c1 > c2) {
+                        return false;
+                    } else if (c1 < c2) {
+                        return true;
+                    }
+                } else {
+                    return true;
+                }
+            } else {
+                if (CharUtils.isCharacter(c2) || CharUtils.isNumber(c2)) {
+                    return false;
+                }
+            }
+        }
+        // 如果字母比较完毕,顺序仍然一致,则比较哪一方还剩有字母未比较
+        if (length1 < length2) {
+            return true;
+        } else if (length1 > length2) {
+            return false;
+        }
+        return false;
+    }
+
+    /**
+     * 交换位置
+     */
+    private void exchange(File[] files, int i, int j) {
+        File temp = files[i];
+        files[i] = files[j];
+        files[j] = temp;
+    }
+
 	/**
 	 * 获取多个文件的信息
 	 *