From a60e87265c02ca0ea295c46f9fbb25b0fd075a0f Mon Sep 17 00:00:00 2001 From: luogw <3132758203@qq.com> Date: Mon, 20 Jul 2026 10:20:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=97=E9=A2=98=E5=88=86=E7=B1=BB=E6=8C=89?= =?UTF-8?q?=E9=A6=96=E5=AD=97=E6=AF=8D=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ClassicCategoryApplicationServiceImpl.java | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java b/src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java index 7f81ec6..31cb4e5 100644 --- a/src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java +++ b/src/main/java/com/project/classicpaper/application/impl/ClassicCategoryApplicationServiceImpl.java @@ -15,6 +15,7 @@ import com.project.classicpaper.domain.service.ClassicPaperSetBaseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.text.Collator; import java.util.*; import java.util.stream.Collectors; @@ -29,11 +30,11 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp @Override public Result> treeList(Boolean withPaperSet) { - // 1. 查所有分类 → DTO - List list = classicCategoryBaseService.list( - new LambdaQueryWrapper().orderByAsc(ClassicCategoryEntity::getSort) - ).stream().map(entity -> entity.toDTO(ClassicCategoryDTO::new)) + // 1. 查所有分类 → DTO(Java 中排序) + List list = classicCategoryBaseService.list() + .stream().map(entity -> entity.toDTO(ClassicCategoryDTO::new)) .collect(Collectors.toList()); + list.sort(this::compareByName); // 2. 构建树 List tree = TreeUtils.buildLongTree(list, @@ -69,6 +70,46 @@ public class ClassicCategoryApplicationServiceImpl implements ClassicCategoryApp return Result.success(tree); } + /** + * 按名称排序规则: + * - 中文 → 按拼音首字母(使用 java Collator) + * - 英文 → 按字母顺序 + * - 其他 → 按 ID + */ + private int compareByName(ClassicCategoryDTO a, ClassicCategoryDTO b) { + String nameA = a.getName(); + String nameB = b.getName(); + int typeA = getCharType(nameA); + int typeB = getCharType(nameB); + if (typeA != typeB) { + return Integer.compare(typeA, typeB); + } + if (typeA == 0) { + return Collator.getInstance(Locale.CHINESE).compare( + nameA != null ? nameA : "", + nameB != null ? nameB : ""); + } else if (typeA == 1) { + String safeA = nameA != null ? nameA : ""; + String safeB = nameB != null ? nameB : ""; + return safeA.compareToIgnoreCase(safeB); + } else { + return Long.compare( + a.getId() != null ? a.getId() : 0L, + b.getId() != null ? b.getId() : 0L); + } + } + + /** + * 判断首字符类型:0-中文,1-英文,2-其他 + */ + private int getCharType(String name) { + if (name == null || name.isEmpty()) return 2; + char first = name.charAt(0); + if (first >= '一' && first <= '鿿') return 0; + if ((first >= 'A' && first <= 'Z') || (first >= 'a' && first <= 'z')) return 1; + return 2; + } + /** * 递归注入 paperSetCount: * 二级节点取直接统计值,一级节点累加子节点