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]