仅在Java中按字符串的首字母顺序对字符串数组进行排序
在这里,我们按字母顺序对字符串数组进行排序,即“John”的“J”将在“Chris”之后,因为“Chris”的第一个字符是“C”。
让我们首先创建一个String数组:
String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };现在,根据第一个字符对字符串数组进行排序:
Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));
下面是一个仅按字母顺序对字符串数组进行字母排序的示例:
示例
import java.util.Arrays;
public class Demo {
public static void main(String[] args) {
String[] strArr = { "PQRS", "AB", "RSTUVW", "RST", "U", "UVWXY", "OUJBG" };
System.out.println("Sorting array strings = ");
Arrays.sort(strArr, (str1, str2) -> str1.charAt(0) - str2.charAt(0));
Arrays.asList(strArr).forEach(System.out::println);
}
}输出结果
Sorting array strings = AB OUJBG PQRS RSTUVW RST U UVWXY
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬