Commit cc0203f6 zshaohui

1.设备互联,按设备名称排序

1 个父辈 935885b8
...@@ -17,6 +17,7 @@ import com.neotel.smfcore.core.device.util.DataCache; ...@@ -17,6 +17,7 @@ import com.neotel.smfcore.core.device.util.DataCache;
import com.neotel.smfcore.core.kanban.rest.bean.dto.*; import com.neotel.smfcore.core.kanban.rest.bean.dto.*;
import com.neotel.smfcore.core.kanban.rest.bean.mapstruct.BoxTaskMapper; import com.neotel.smfcore.core.kanban.rest.bean.mapstruct.BoxTaskMapper;
import com.neotel.smfcore.core.kanban.rest.bean.query.BoxTaskQueryCriter; import com.neotel.smfcore.core.kanban.rest.bean.query.BoxTaskQueryCriter;
import com.neotel.smfcore.core.kanban.rest.utils.NaturalOrderComparator;
import com.neotel.smfcore.core.message.util.DeviceMessageUtil; import com.neotel.smfcore.core.message.util.DeviceMessageUtil;
import com.neotel.smfcore.core.msd.bean.MSDSettiings; import com.neotel.smfcore.core.msd.bean.MSDSettiings;
import com.neotel.smfcore.core.solder.util.SolderBoxCache; import com.neotel.smfcore.core.solder.util.SolderBoxCache;
...@@ -107,7 +108,7 @@ public class BoxKanbanController { ...@@ -107,7 +108,7 @@ public class BoxKanbanController {
} }
} }
if(boxStatusDtos.size()>0){ if(boxStatusDtos.size()>0){
boxStatusDtos = boxStatusDtos.stream().sorted(Comparator.comparing(BoxStatusDto :: getName)).collect(Collectors.toList()); boxStatusDtos = boxStatusDtos.stream().sorted(Comparator.comparing(BoxStatusDto :: getName, new NaturalOrderComparator())).collect(Collectors.toList());
GroupStatusDto groupStatusDto = new GroupStatusDto(group.getId(),group.getGroupName(),boxStatusDtos,groupType); GroupStatusDto groupStatusDto = new GroupStatusDto(group.getId(),group.getGroupName(),boxStatusDtos,groupType);
groupStatusDtos.add(groupStatusDto); groupStatusDtos.add(groupStatusDto);
} }
......
package com.neotel.smfcore.core.kanban.rest.utils;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 自然排序比较器:字母按字典序,数字按数值大小排序
*/
public class NaturalOrderComparator implements Comparator<String> {
// 正则表达式:拆分字符串为"非数字"和"数字"交替的部分
private static final Pattern PATTERN = Pattern.compile("(\\D+)|(\\d+)");
@Override
public int compare(String s1, String s2) {
if (s1 == null && s2 == null) return 0;
if (s1 == null) return -1; // null放前面
if (s2 == null) return 1;
Matcher m1 = PATTERN.matcher(s1);
Matcher m2 = PATTERN.matcher(s2);
// 逐个匹配"非数字/数字"片段并比较
while (m1.find() && m2.find()) {
String group1 = m1.group();
String group2 = m2.group();
// 判断当前片段是数字还是非数字
if (group1.matches("\\d+") && group2.matches("\\d+")) {
// 数字片段:按数值大小比较(避免"10" < "2"的字符串排序问题)
Long num1 = Long.parseLong(group1);
Long num2 = Long.parseLong(group2);
int numCompare = num1.compareTo(num2);
if (numCompare != 0) {
return numCompare;
}
} else {
// 非数字片段:按字典序比较
int strCompare = group1.compareTo(group2);
if (strCompare != 0) {
return strCompare;
}
}
}
// 一个字符串是另一个的前缀,短的放前面
return Integer.compare(s1.length(), s2.length());
}
}
\ No newline at end of file \ No newline at end of file
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!