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]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP