package sort;
public class CostDto
{
private String projectId;
private String projectName;
private Double subjectMoney;
private Double dySubjectMoney;
private Double result;
public CostDto(){}
public CostDto(String projectId, String projectName, Double subjectMoney, Double dySubjectMoney) {
super();
this.projectId = projectId;
this.projectName = projectName;
this.subjectMoney = subjectMoney;
this.dySubjectMoney = dySubjectMoney;
}
public Double getDySubjectMoney() {
return dySubjectMoney;
}
public void setDySubjectMoney(Double dySubjectMoney) {
this.dySubjectMoney = dySubjectMoney;
}
public String getProjectId() {
return projectId;
}
public void setProjectId(String projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public Double getSubjectMoney() {
return subjectMoney;
}
public void setSubjectMoney(Double subjectMoney) {
this.subjectMoney = subjectMoney;
}
public Double getResult() {
return result;
}
public void setResult(Double result) {
this.result = result;
}
}
package sort;
import java.util.Comparator;
public class CostDtoComparator implements Comparator {
public int compare(Object arg0, Object arg1) {
CostDto obj1 = (CostDto) arg0;
CostDto obj2 = (CostDto) arg1;
if (obj1.getResult() > obj2.getResult()) {
return 1;
} else if (obj1.getResult() == obj2.getResult()) {
return 0;
} else {
return -1;
}
}
}
package sort;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortList {
public static void main(String[] args) {
CostDtoComparator comparator = new CostDtoComparator();
List<CostDto> stList = getCostDtoList();
for(int i = 0; i < stList.size(); i++){
CostDto cost = (CostDto)stList.get(i);
cost.setResult(cost.getDySubjectMoney()-cost.getSubjectMoney());
}
Collections.sort(stList, comparator);
for(int j = 0; j < stList.size(); j++){
CostDto costDto = (CostDto)stList.get(j);
System.out.println(costDto.getProjectId() + " " + costDto.getResult());
}
}
public static List<CostDto> getCostDtoList(){
ArrayList<CostDto> costDtoList = new ArrayList<CostDto>();
CostDto costDto1 = new CostDto("3","pro1",1200,1500);
CostDto costDto2 = new CostDto("2","pro2",200,500);
CostDto costDto3 = new CostDto("6","pro6",100,800);
CostDto costDto5 = new CostDto("1","pro1",550,900);
CostDto costDto6 = new CostDto("8","pro8",800,2000);
costDtoList.add(costDto1);
costDtoList.add(costDto2);
costDtoList.add(costDto3);
costDtoList.add(costDto5);
costDtoList.add(costDto6);
return costDtoList;
}
}