Collections.sort(list, new Comparator<String>(){
public int compare(String o1, String o2) {
return o1.length()-o2.length();
}});
释:o1,o2,即为list里面的对象;list为集合,按照对象的length长度升序排序(反过来降序),如果为复杂对象,length可以换成对象里具体字段。例如:员工对象,按照工资排序。如下:
Collections.sort(list, new Comparator<Emp >(){
public int compare(Emp o1, Emp o2) {
return o1.getSalary()-o2.getSalary();
}});
转载自https://blog.csdn.net/halfgap/article/details/81710709?utm_source=blogxgwz9