java根据某列排序问题比如我有个POJO类,Student(SID,英语,英语排名);现在有一个List:001 ,99,95,90,英语排名002,98 ,90,85,英语排名.我想写个方法,根据list里各个pojo类里各科的分数来算出他们各科的排名,更新到POJO类里.编程语言:java,框架SSH要求方法思路清晰,效率高,稳定
2019-06-19
java根据某列排序问题
比如我有个POJO类,Student(SID,英语,英语排名);现在有一个List:001 ,99,95,90,英语排名
002,98 ,90,85,英语排名
.
我想写个方法,根据list里各个pojo类里各科的分数来算出他们各科的排名,更新到POJO类里.
编程语言:java,框架SSH
要求方法思路清晰,效率高,稳定
优质解答
//主要就是个排序问题,实现如下:
public static void main(String[] args) throws Exception {
List<Student> listA = new ArrayList<Student>();
Student p1 = new Student();
Student p2 = new Student();
Student p3 = new Student();
p1.setId("001");
p1.setYuwen(86);
p1.setShuxue(55);
p1.setYingyu(77);
p2.setId("002");
p2.setYuwen(83);
p2.setShuxue(57);
p2.setYingyu(55);
p3.setId("003");
p3.setYuwen(88);
p3.setShuxue(55);
p3.setYingyu(99);
listA.add(p1);
listA.add(p2);
listA.add(p3);
mysort(listA);
for (Student s:listA) {
System.out.println(s.getId()+"\t"+s.getYuwen()+"\t"+s.getYuwennum()+"\t"+s.getShuxue()+"\t"+s.getShuxueum()+"\t"+s.getYingyu()+"\t"+s.getYingyuum());
}
}
public static List<Student> mysort(List<Student> listA){
//语文排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getYuwen();
int hits1 = arg1.getYuwen();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setYuwennum(i+1);
}
//数学排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getShuxue();
int hits1 = arg1.getShuxue();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setShuxueum(i+1);
}
//英语排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getYingyu();
int hits1 = arg1.getYingyu();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setYingyuum(i+1);
}
return listA;
}
static class Student {
private String id;
private Integer yuwen;
private Integer yuwennum;
private Integer shuxue;
private Integer shuxueum;
private Integer yingyu;
private Integer yingyuum;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getYuwen() {
return yuwen;
}
public void setYuwen(Integer yuwen) {
this.yuwen = yuwen;
}
public Integer getYuwennum() {
return yuwennum;
}
public void setYuwennum(Integer yuwennum) {
this.yuwennum = yuwennum;
}
public Integer getShuxue() {
return shuxue;
}
public void setShuxue(Integer shuxue) {
this.shuxue = shuxue;
}
public Integer getShuxueum() {
return shuxueum;
}
public void setShuxueum(Integer shuxueum) {
this.shuxueum = shuxueum;
}
public Integer getYingyu() {
return yingyu;
}
public void setYingyu(Integer yingyu) {
this.yingyu = yingyu;
}
public Integer getYingyuum() {
return yingyuum;
}
public void setYingyuum(Integer yingyuum) {
this.yingyuum = yingyuum;
}
}
//主要就是个排序问题,实现如下:
public static void main(String[] args) throws Exception {
List<Student> listA = new ArrayList<Student>();
Student p1 = new Student();
Student p2 = new Student();
Student p3 = new Student();
p1.setId("001");
p1.setYuwen(86);
p1.setShuxue(55);
p1.setYingyu(77);
p2.setId("002");
p2.setYuwen(83);
p2.setShuxue(57);
p2.setYingyu(55);
p3.setId("003");
p3.setYuwen(88);
p3.setShuxue(55);
p3.setYingyu(99);
listA.add(p1);
listA.add(p2);
listA.add(p3);
mysort(listA);
for (Student s:listA) {
System.out.println(s.getId()+"\t"+s.getYuwen()+"\t"+s.getYuwennum()+"\t"+s.getShuxue()+"\t"+s.getShuxueum()+"\t"+s.getYingyu()+"\t"+s.getYingyuum());
}
}
public static List<Student> mysort(List<Student> listA){
//语文排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getYuwen();
int hits1 = arg1.getYuwen();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setYuwennum(i+1);
}
//数学排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getShuxue();
int hits1 = arg1.getShuxue();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setShuxueum(i+1);
}
//英语排名
Collections.sort(listA, new Comparator<Student>() {
public int compare(Student arg0, Student arg1) {
int hits0 = arg0.getYingyu();
int hits1 = arg1.getYingyu();
if (hits1 > hits0) {
return 1;
} else if (hits1 == hits0) {
return 0;
} else {
return -1;
}
}
});
for (int i = 0; i < listA.size(); i++) {
listA.get(i).setYingyuum(i+1);
}
return listA;
}
static class Student {
private String id;
private Integer yuwen;
private Integer yuwennum;
private Integer shuxue;
private Integer shuxueum;
private Integer yingyu;
private Integer yingyuum;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getYuwen() {
return yuwen;
}
public void setYuwen(Integer yuwen) {
this.yuwen = yuwen;
}
public Integer getYuwennum() {
return yuwennum;
}
public void setYuwennum(Integer yuwennum) {
this.yuwennum = yuwennum;
}
public Integer getShuxue() {
return shuxue;
}
public void setShuxue(Integer shuxue) {
this.shuxue = shuxue;
}
public Integer getShuxueum() {
return shuxueum;
}
public void setShuxueum(Integer shuxueum) {
this.shuxueum = shuxueum;
}
public Integer getYingyu() {
return yingyu;
}
public void setYingyu(Integer yingyu) {
this.yingyu = yingyu;
}
public Integer getYingyuum() {
return yingyuum;
}
public void setYingyuum(Integer yingyuum) {
this.yingyuum = yingyuum;
}
}