Browse Source

Improved file name sorting.

master
Christian P. MOMON 8 months ago
parent
commit
4cf6678aae
  1. 45
      src/org/april/logar/util/CompareUtils.java
  2. 2
      src/org/april/logar/util/FileComparator.java
  3. 10
      src/org/april/logar/util/Files.java

45
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("(?<left>.*)(?<digits>\\d+)(?<right>.*)");
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.
*

2
src/org/april/logar/util/FileComparator.java

@ -89,7 +89,7 @@ public class FileComparator implements Comparator<File>
{
default:
case NAME:
result = CompareUtils.compare(getName(alpha), getName(bravo));
result = CompareUtils.compareNatural(getName(alpha), getName(bravo));
break;
}
}

10
src/org/april/logar/util/Files.java

@ -240,6 +240,16 @@ public class Files extends ArrayList<File>
return result;
}
public Files sortByPathname()
{
Files result;
result = sort(FileComparator.Sorting.NAME);
//
return result;
}
/**
* Of.
*

Loading…
Cancel
Save