From 4cf6678aae7fce79e40a41c49b9a625736451136 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Sun, 25 Apr 2021 04:07:20 +0200 Subject: [PATCH] Improved file name sorting. --- src/org/april/logar/util/CompareUtils.java | 45 ++++++++++++++++++++ src/org/april/logar/util/FileComparator.java | 2 +- src/org/april/logar/util/Files.java | 10 +++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/org/april/logar/util/CompareUtils.java b/src/org/april/logar/util/CompareUtils.java index 62e22ac..fad6f9a 100644 --- a/src/org/april/logar/util/CompareUtils.java +++ b/src/org/april/logar/util/CompareUtils.java @@ -19,6 +19,8 @@ package org.april.logar.util; import java.time.LocalDateTime; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -235,6 +237,49 @@ public class CompareUtils return result; } + /** + * Compare natural. + * + * @param alpha + * the alpha + * @param bravo + * the bravo + * @return the int + */ + public static int compareNatural(final String alpha, final String bravo) + { + int result; + + Pattern pattern = Pattern.compile("(?.*)(?\\d+)(?.*)"); + + Matcher matcherA = pattern.matcher(alpha); + Matcher matcherB = pattern.matcher(bravo); + if ((matcherA.find()) && (matcherB.find())) + { + result = StringUtils.compare(matcherA.group("left"), matcherB.group("left")); + + if (result == 0) + { + Long a = Long.valueOf(matcherA.group("digits")); + Long b = Long.valueOf(matcherB.group("digits")); + + result = compare(a, b); + + if (result == 0) + { + result = compareNatural(matcherA.group("right"), matcherB.group("right")); + } + } + } + else + { + result = StringUtils.compare(alpha, bravo); + } + + // + return result; + } + /** * Compare reverse. * diff --git a/src/org/april/logar/util/FileComparator.java b/src/org/april/logar/util/FileComparator.java index 23de8b7..9afc4f3 100644 --- a/src/org/april/logar/util/FileComparator.java +++ b/src/org/april/logar/util/FileComparator.java @@ -89,7 +89,7 @@ public class FileComparator implements Comparator { default: case NAME: - result = CompareUtils.compare(getName(alpha), getName(bravo)); + result = CompareUtils.compareNatural(getName(alpha), getName(bravo)); break; } } diff --git a/src/org/april/logar/util/Files.java b/src/org/april/logar/util/Files.java index cb21101..04a4bc4 100644 --- a/src/org/april/logar/util/Files.java +++ b/src/org/april/logar/util/Files.java @@ -240,6 +240,16 @@ public class Files extends ArrayList return result; } + public Files sortByPathname() + { + Files result; + + result = sort(FileComparator.Sorting.NAME); + + // + return result; + } + /** * Of. *