【发布时间】:2021-06-25 10:54:47
【问题描述】:
假设我有一个列表 ["d", "a", "y / a", "a / z", "b / a / c", "a / c / a"] 我需要用下一种方式对其进行排序:
- 应按斜线数排序
- 之后应该按字母排序 结果: [“a”、“d”、“a / z”、“y / a”、“a / c / a”,“b / a / c”]
第一部分正在处理这个:
Comparator<String> comparator = Comparator.comparing(s -> StringUtils.countMatches(s, "/"));
map.keySet().stream().sorted(comparator).forEach(System.out::println);
但结果: [“d”、“a”、“y / a”、“a / z”、“b / a / c”,“a / c / a”]
【问题讨论】:
-
StringUtils.countMatches(s, "//")应该是StringUtils.countMatches(s, "/") -
是的,为什么要使用 2 个斜杠?
-
这能回答你的问题吗? How to sort an ArrayList using two sorts
-
和
comparing(可能是comparingInt( -
StringUtils.countMatches(s, "/")可能是s.split("/").length。
标签: java sorting collections