添加 Windows 资源管理器风格字符串比较器

This commit is contained in:
Looly 2024-06-18 16:01:40 +08:00
parent bd164db9b4
commit 8bb736b375
3 changed files with 15 additions and 7 deletions

View File

@ -12,6 +12,7 @@
* 【csv 】 CsvWriter.writeBeans增加重载可选是否写出表头issue#IA57W2@Gitee * 【csv 】 CsvWriter.writeBeans增加重载可选是否写出表头issue#IA57W2@Gitee
* 【core 】 BetweenFormatter支持自定义设置单位pr#1228@Gitee * 【core 】 BetweenFormatter支持自定义设置单位pr#1228@Gitee
* 【cache 】 Cache.put变更策略对于替换的键值对不清理队列issue#3618@Github * 【cache 】 Cache.put变更策略对于替换的键值对不清理队列issue#3618@Github
* 【core 】 添加 Windows 资源管理器风格字符串比较器pr#3620@Github
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复AnnotationUtil可能的空指针错误 * 【core 】 修复AnnotationUtil可能的空指针错误

View File

@ -1,5 +1,7 @@
package cn.hutool.core.comparator; package cn.hutool.core.comparator;
import cn.hutool.core.util.StrUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
@ -19,15 +21,19 @@ import java.util.regex.Pattern;
* "abc12.doc" }}这与在资源管理器中看到的相同</p> * "abc12.doc" }}这与在资源管理器中看到的相同</p>
* *
* @author YMNNs * @author YMNNs
* @see * @see <a href="https://stackoverflow.com/questions/23205020/java-sort-strings-like-windows-explorer">Java - Sort Strings like Windows Explorer</a>
* <a href="https://stackoverflow.com/questions/23205020/java-sort-strings-like-windows-explorer">Java - Sort Strings like Windows Explorer</a>
*/ */
public class WindowsExplorerStringComparator implements Comparator<String> { public class WindowsExplorerStringComparator implements Comparator<CharSequence> {
/**
* 单例
*/
public static final WindowsExplorerStringComparator INSTANCE = new WindowsExplorerStringComparator();
private static final Pattern splitPattern = Pattern.compile("\\d+|\\.|\\s"); private static final Pattern splitPattern = Pattern.compile("\\d+|\\.|\\s");
@Override @Override
public int compare(String str1, String str2) { public int compare(CharSequence str1, CharSequence str2) {
Iterator<String> i1 = splitStringPreserveDelimiter(str1).iterator(); Iterator<String> i1 = splitStringPreserveDelimiter(str1).iterator();
Iterator<String> i2 = splitStringPreserveDelimiter(str2).iterator(); Iterator<String> i2 = splitStringPreserveDelimiter(str2).iterator();
while (true) { while (true) {
@ -65,16 +71,16 @@ public class WindowsExplorerStringComparator implements Comparator<String> {
} }
} }
private List<String> splitStringPreserveDelimiter(String str) { private List<String> splitStringPreserveDelimiter(CharSequence str) {
Matcher matcher = splitPattern.matcher(str); Matcher matcher = splitPattern.matcher(str);
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
int pos = 0; int pos = 0;
while (matcher.find()) { while (matcher.find()) {
list.add(str.substring(pos, matcher.start())); list.add(StrUtil.sub(str, pos, matcher.start()));
list.add(matcher.group()); list.add(matcher.group());
pos = matcher.end(); pos = matcher.end();
} }
list.add(str.substring(pos)); list.add(StrUtil.subSuf(str, pos));
return list; return list;
} }
} }

View File

@ -12,6 +12,7 @@ import java.util.List;
* *
* @author YMNNs * @author YMNNs
*/ */
@SuppressWarnings("serial")
public class WindowsExplorerStringComparatorTest { public class WindowsExplorerStringComparatorTest {
List<String> answer1 = new ArrayList<String>() {{ List<String> answer1 = new ArrayList<String>() {{