[英]Sort a list of strings in java in alphabetical order
I have a list with some strings in this format:我有一个包含这种格式的一些字符串的列表:
List<String> ids = new ArrayList<>();
ids.add("B-7");
ids.add("B-5");
ids.add("A-3");
ids.add("B-8");
ids.add("B-1");
ids.add("B-6");
ids.add("B-2");
ids.add("B-3");
ids.add("B-10");
ids.add("A-1");
ids.add("B-4");
ids.add("B-9");
ids.add("A-2");
I need to sort it to have this output, (iterating over the list):我需要对它进行排序以获得这个 output,(遍历列表):
A-1
A-2
A-3
B-1
B-2
B-3
B-4
B-5
B-6
B-7
B-8
B-9
B-10
I am using:我在用:
List<String> sortedIds = ids.stream().sorted().collect(Collectors.toList());
But instead, my output:但相反,我的 output:
A-1
A-2
A-3
B-1
B-10 -- Error
B-2
B-3
B-4
B-5
B-6
B-7
B-8
B-9
You can create a custom Comparator
using Comparator.comparing
and Comparator.thenComparing
.您可以使用Comparator.comparing
和Comparator.thenComparing
Comparator
List<String> sortedIds = ids.stream().sorted(
Comparator.comparing((String s) -> s.substring(0, s.indexOf('-')))
.thenComparingInt(s -> Integer.parseInt(s.substring(s.indexOf('-') + 1))))
.collect(Collectors.toList());
The default Comparator
is going to operate in lexicographical order.默认的Comparator
将按字典顺序运行。 You need to compare the String parts and Integer parts separately.您需要分别比较 String 部分和 Integer 部分。 Something like就像是
Collections.sort(ids, (a, b) -> {
String[] at = a.split("-");
String[] bt = b.split("-");
int c = at[0].compareTo(bt[0]);
if (c != 0) {
return c;
}
return Integer.valueOf(Integer.parseInt(at[1])).compareTo(Integer.parseInt(bt[1]));
});
What you'll need is a customer Comparator<String>
to use inside of the sorted()
intermediate operation您需要在sorted()
中间操作中使用客户Comparator<String>
List<String> sorted = ids.stream().sorted((o1, o2) -> {
String[] first = o1.split("-");
String[] second = o2.split("-");
int lettersComparison = first[0].compareTo(second[0]);
if (lettersComparison != 0) {
return lettersComparison;
}
Integer firstNumber = Integer.valueOf(first[1]);
Integer secondNumber = Integer.valueOf(second[1]);
return firstNumber.compareTo(secondNumber);
}).toList();
Which outputs哪个输出
[A-1, A-2, A-3, B-1, B-2, B-3, B-4, B-5, B-6, B-7, B-8, B-9, B-10]
That said, if you want to just sort the existing List
, I would suggest not to go through a Stream
for that because it has an overhead in terms of performance and creation of new object.也就是说,如果您只想对现有的List
进行排序,我建议不要通过Stream
对 go 进行排序,因为它在性能和创建新 ZA8CFDE6331BD59EB2AC966F8911C4B 方面有开销。
You can use list.sort(comparator)
and use the same Comparator<String>
as hereabove您可以使用list.sort(comparator)
并使用与上述相同的Comparator<String>
ids.sort((o1, o2) -> {
String[] first = o1.split("-");
String[] second = o2.split("-");
int lettersComparison = first[0].compareTo(second[0]);
if (lettersComparison != 0) {
return lettersComparison;
}
Integer firstNumber = Integer.valueOf(first[1]);
Integer secondNumber = Integer.valueOf(second[1]);
return firstNumber.compareTo(secondNumber);
});
Try this.尝试这个。
List<String> ids = Arrays.asList(
"B-7", "B-5", "A-3", "B-8", "B-1", "B-6", "B-2",
"B-3", "B-10", "A-1", "B-4", "B-9", "A-2");
Collections.sort(ids,
Comparator.comparingInt(String::length)
.thenComparing(Function.identity()));
System.out.println(ids);
output output
[A-1, A-2, A-3, B-1, B-2, B-3, B-4, B-5, B-6, B-7, B-8, B-9, B-10]
Here is yet another way.这是另一种方式。 I tend to split first and then glue together after sorting rather than continually splitting inside a comparator.我倾向于先拆分,然后在排序后粘合在一起,而不是在比较器中不断拆分。
List<String> result = ids.stream().map(s -> s.split("-"))
.sorted(Comparator.comparing((String[] a) -> a[0])
.thenComparingInt(
a -> Integer.parseInt(a[1])))
.map(a -> String.join(",", a))
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.