怎么对一个List集合进行排序?
直接调用List集合自身的.sort方法
String[] string=new String[]{"s1","A2","a3","B4","c5"};
List<String> list = new ArrayList<> (Arrays.asList(string));
//正序
list.sort(String::compareTo);
//倒序
list.sort(Collections.reverseOrder());
正序输出:[A2, B4, a3, c5, s1]
倒序输出:[s1, c5, a3, B4, A2]
使用工具类:Collections.sort();
String[] string=new String[]{"s1","A2","a3","B4","c5"};
List<String> list = new ArrayList<> (Arrays.asList(string));
//正序
Collections.sort(list);
//倒序
Collections.reverse(list);
正序输出:[A2, B4, a3, c5, s1]
倒序输出:[s1, c5, a3, B4, A2]
stream的sort方法
String[] string=new String[]{"s1","A2","a3","B4","c5"};
List<String> list = new ArrayList<> (Arrays.asList(string));
//正序
list.stream().sorted();
//倒序
List<String> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println(collect);
正序输出:[A2, B4, a3, c5, s1]
倒序输出:[s1, c5, a3, B4, A2]