排序

package com.doit.day25;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;


public class PaiXuDemo12 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList<Student52> al = new ArrayList<Student52>();
		for (int i = 1; i < 4; i++) {
			System.out.println("请输入第" + i + "个学生的姓名");
			String name = sc.next();
			System.out.println("请输入第" + i + "个学生的学号");
			int id = sc.nextInt();
			System.out.println("请输入第" + i + "个学生的语文成绩");
			int chinese = sc.nextInt();
			System.out.println("请输入第" + i + "个学生的历史成绩");
			int history = sc.nextInt();
			System.out.println("请输入第" + i + "个学生的数学成绩");
			int math = sc.nextInt();
			Student52 stu = new Student52(name, id, chinese, math, history);
			al.add(stu);
		}
		
		System.out.println(al);
		
		Collections.sort(al,new Comparator<Student52>(){

			@Override
			public int compare(Student52 o1, Student52 o2) {
				/*if(o2.getScore() == o1.getScore()){
					return o1.name.compareTo(o2.name);
				}else if(o1.name.equals(o2.name)){
					return o1.id - o2.id;
				}
				return o2.getScore() - o1.getScore();*/
				int i = o2.getScore() - o1.getScore();
				int i1 = i == 0 ? o1.name.compareTo(o2.name) : i;
				int i2 = i1 == 0 ? o1.id - o2.id : i1;
				return i2;
				
			}
			
		});
		
		System.out.println(al);
		
		
	}

}

class Student52{
	String name;
	int id;
	int chinese;
	int history;
	int math;
	public Student52() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student52(String name, int id, int chinese, int math, int history) {
		super();
		this.name = name;
		this.id = id;
		this.chinese = chinese;
		this.math = math;
		this.history = history;
	}
	@Override
	public String toString() {
		return "Student52 [name=" + name + ", id=" + id + ", chinese=" + chinese + ", math=" + math + ", history="
				+ history + "]";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	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 getHistory() {
		return history;
	}
	public void setHistory(int history) {
		this.history = history;
	}
	public int getScore(){
		return this.chinese + this.history + this.math;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值