直接上代码:
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
/**
* <b>Function: </b> todo
*
* @program: 泛型排序
* @Package: com.kingbal.king.dmp
* @author: songjianlin
* @date: 2024/09/12
* @version: 1.0
* @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
*/
public class StrObject {
int sortField;
public StrObject (int sortField) {
this.sortField = sortField;
}
public int getSortField() {
return sortField;
}
}
public class SortStr {
public static void main(String[] args) {
List<StrObject > list = new ArrayList<>();
list.add(new StrObject(3));
list.add(new StrObject(1));
list.add(new StrObject(2));
// 按sortField字段降序排序
Collections.sort(list, new Comparator<StrObject>() {
@Override
public int compare(StrObject o1, StrObject o2) {
return Integer.compare(o2.getSortField(), o1.getSortField());
}
});
// 打印排序结果
for (StrObjectobj : list) {
System.out.println(obj.getSortField());
}
}
}