Improved file name sorting.

This commit is contained in:
Christian P. MOMON 2021-04-25 04:07:20 +02:00
parent 77837b3aee
commit 4cf6678aae
3 changed files with 56 additions and 1 deletions

View File

@ -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.
*

View File

@ -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;
}
}

View File

@ -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.
*