62,626
社区成员
发帖
与我相关
我的任务
分享
package csdn;
import java.util.ArrayList;
public class Student {
private int no;
private String name;
private int chinese;
private int math;
public Student(int no, String name, int chinese, int math) {
this.no = no;
this.name = name;
this.chinese = chinese;
this.math = math;
}
public int getNo() {
return no;
}
public String getName() {
return name;
}
public int getChinese() {
return chinese;
}
public int getMath() {
return math;
}
public int getScores() {
return this.chinese + this.math;
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append("no=").append(no);
sb.append(",name=").append(name);
sb.append(",chinese=").append(chinese);
sb.append(",math=").append(math);
sb.append(",scores=").append(this.getScores());
sb.append("]");
return sb.toString();
}
public static void sortProcess(ArrayList<Student> stuList) {
int size = stuList.size();
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((stuList.get(j).getScores() < stuList.get(i).getScores())
|| ((stuList.get(j).getScores() == stuList.get(i).getScores())
&& (stuList.get(j).getChinese() < stuList.get(i).getChinese()))) {
Student temp = stuList.get(j);
stuList.set(j, stuList.get(i));
stuList.set(i, temp);
}
}
}
for (Student stu : stuList) {
System.out.println(stu);
}
}
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<Student>();
Student stu1 = new Student(1, "张三", 80, 85);
Student stu2 = new Student(2, "李四", 95, 83);
Student stu3 = new Student(3, "王五", 85, 80);
Student stu4 = new Student(4, "赵六", 82, 83);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
sortProcess(stuList);
}
}
package com.example.demo.vo;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class Student implements Serializable {
private int no;
private String name;
private int chinese;
private int math;
private int sum;
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getSum() {
return sum;
}
public void setSum(int sum) {
this.sum = sum;
}
@Override
public String toString() {
return "Student{" +
"no=" + no +
", name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", sum=" + sum +
'}';
}
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setNo(1);
student1.setName("name1");
student1.setChinese(90);
student1.setMath(92);
student1.setSum(student1.getChinese()+student1.getMath());
list.add(student1);
Student student2 = new Student();
student2.setNo(2);
student2.setName("name2");
student2.setChinese(90);
student2.setMath(94);
student2.setSum(student2.getChinese()+student2.getMath());
list.add(student2);
Student student3 = new Student();
student3.setNo(3);
student3.setName("name3");
student3.setChinese(95);
student3.setMath(89);
student3.setSum(student3.getChinese()+student3.getMath());
list.add(student3);
Student student4 = new Student();
student4.setNo(4);
student4.setName("name4");
student4.setChinese(96);
student4.setMath(92);
student4.setSum(student4.getChinese()+student4.getMath());
list.add(student4);
List<Student> list2 = list
.stream()
.sorted(Comparator.comparing(Student::getSum).thenComparing(Student::getChinese).reversed())
.collect(Collectors.toList());
System.out.print(list2);
}
}