1、实现java.util.Comparator接口,重写int compare(Object o1, Object o2)方法即可
public class CompareUtil implements Comparator<Object> {
/**
* 重新排序方法,这里是根据complexity参数,从大到小排序
*/
@Override
public int compare(Object o1, Object o2) {
OwerProjectVO s1 = (OwerProjectVO) o1;
OwerProjectVO s2 = (OwerProjectVO) o2;
if (s1.getComplexity() < s2.getComplexity()) {
return 1;
}
if (s1.getComplexity() > s2.getComplexity()) {
return -1;
}
return 0;
}
}
2、OwerProjectVO类代码
public class OwerProjectVO {
/**
* 负责人姓名
*/
private String name;
/**
* 圈复杂度
*/
private double complexity;
/**
* 项目名称列表
*/
private List<String> projectList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getComplexity() {
return complexity;
}
public void setComplexity(double complexity) {
this.complexity = complexity;
}
public List<String> getProjectList() {
return projectList;
}
public void setProjectList(List<String> projectList) {
this.projectList = projectList;
}
}
3、调用进行排序
Collections.sort(owerProjectVOList, new CompareUtil());