【蓝桥云课】Java对象排序

该程序使用Java编程,通过实现Comparable接口对一个学生对象列表进行排序。首先按照总成绩降序排列,当总成绩相同时,依据Java成绩再次进行降序排序。列表中的每个学生对象包含姓名、Java成绩、MySQL成绩和总成绩属性。
摘要由CSDN通过智能技术生成

题目:对下列的成绩表按总成绩进行降序排序,如果总成绩相同时按Java成绩降序排序。

姓名Java成绩MySQL成绩总成绩
张三10059159
李四100100200
王五9896194
赵六9698194
陈七59100159

程序代码:

import java.util.ArrayList;
import java.util.Collections;

public class ObjectSort {

	public static void main(String[] args) {
		ArrayList<Student> list = new ArrayList<Student>();
		list.add(new Student("张三",100,59));
		list.add(new Student("李四",100,100));
		list.add(new Student("王五",98,96));
		list.add(new Student("赵六",96,98));
		list.add(new Student("陈七",59,100));
		Collections.sort(list);
		for(int i=0;i<list.size();i++) {
			System.out.println(list.get(i));
		}
	}
}

class Student implements Comparable<Student>{
	String student_name;
	int java_score;
	int mysql_score;
	int total_score;
	
	public Student(String student_name,int java_score,int mysql_score) {
		this.student_name = student_name;
		this.java_score = java_score;
		this.mysql_score = mysql_score;
		this.total_score = java_score+mysql_score;
	}
	
	@Override
	public String toString() {
		return "Student [student_name=" + student_name + ", java_score=" + java_score + ", mysql_score=" + mysql_score
				+ ", total_score=" + total_score + "]";
	}

	@Override
	public int compareTo(Student other) {
		if(this.total_score==other.total_score) {
			return other.java_score-this.java_score;
		} else {
			return other.total_score-this.total_score;
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CS_木成河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值