Java程序以不区分大小写的顺序对列表进行排序
假设您的列表包含以下元素-
P, W, g, K, H, t, E
因此,大小写敏感的顺序方式,大写和小写字母都将被考虑,与大小写无关。输出将是-
E, g, H, K, P, t, W
以下是我们的数组-
String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };将上面的数组转换为列表-
List<String>list = Arrays.asList(arr);
现在以不区分大小写的顺序对上面的列表进行排序-
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
示例
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] argv) throws Exception {
String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };
List<String>list = Arrays.asList(arr);
System.out.println("List = "+list);
Collections.sort(list);
System.out.println("Case Sensitive Sort = "+list);
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println("Case Insensitive Sort = "+list);
}
}输出结果
List = [P, W, g, K, H, t, E] Case Sensitive Sort = [E, H, K, P, W, g, t] Case Insensitive Sort = [E, g, H, K, P, t, W]
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短