Java实现集合排序(以学生成绩排序为例)

信息学院年终评定奖学金,需要对整个年级的学生按照平均分数进行排名。 要求:根据输入的学号和平均成绩,按照平均成绩降序输出学号,如果平均成绩相同,按照输入的顺序输出。

比如将一个List<Student>排序,则有两种方式:
1:Student实现Comparable接口:
2:给排序方法传递一个Comparator参数:

- 本文以对Student对象集合为例进行排序
Java通过Collections.sort(List<Student> stuList)和Collections.sort(List<Student> stuList,Comparator c)两种方法实现排序。

用Collections.sort(List list) 方法实现排序:

step1: 确保Student类实现了Comparable接口,并重写了compareTo()方法。

step2:调用Collections.sort(List list) 方法进行排序。

package Test;

/*3 .信息学院年终评定奖学金,需要对整个年级的学生按照平均分数进行排名。
要求:根据输入的学号和平均成绩,按照平均成绩降序输出学号,如果平均成绩相同,按照输入的顺序输出。
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Test_3 {

class Student implements Comparable<Student> {
	private String sno;
	private int score;

	public Student(String sno, int score) {
		this.sno = sno;
		this.score = score;
	}

	public String getSno() {
		return sno;
	}

	public void setSno(String sno) {
		this.sno = sno;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int compareTo(Student s) {
//	int compareTo(T o)将此对象与指定的对象进行比较以进行排序。 返回一个负整数,零或正整数,因为该对象小于,等于或大于指定对象。
		return s.getScore() - this.getScore();
	}
public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Student> students = new ArrayList<Student>();
		for (int i = 1; i <= n; i++) {
			Student student = new Student(sc.next(), sc.nextInt());
			students.add(student);
		}
		Collections.sort(students);
		for (Student student : students) {
			System.out.println(student.getSno());
		}
	}
}

}

用Collections.sort(List list,Comparator c) 方法实现排序:

该方法传入一个比较器,用于比较各元素的大小。该方法不需要元素实现Comparable接口,但需要一个实现Comparator接口的实现类来实例化一个比较器,注意,这里的Comparator是一个接口而非类。这里通常采用匿名内部类的方法。

public class Student {
	private String id;
	private double grade;
	public Student(String id, double grade) {
		this.id = id;
		this.grade = grade;
	}
	public Student() {}
	public String getId() {
		return this.id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public double getGrade() {
		return this.grade;
	}
	public void setGrade(double grade) {
		this.grade = grade;
	}
	public String toString() {
		return "Student [id=" + id + ", grade=" + grade + "]";
	}
}




import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
 
public class Sop {
	public static void main(String[] args)
	{
		List<Student>list=new LinkedList<Student>();
		int n;
		Scanner cin=new Scanner(System.in);
		System.out.println("输入学生的总人数");
		n=cin.nextInt();
		System.out.println("输入学生的学号,平均成绩");
		for(int i=0;i<n;i++)
		{
			String id=cin.next();
			double grade=cin.nextDouble();
			Student student=new Student(id, grade);
			list.add(student);
		}
 
		cin.close();
       Collections.sort(list,new Comparator<Student>()
       {
 
            public int compare(Student stu1, Student stu2)
            {
 
               return (int) (stu2.getGrade()-stu1.getGrade());
 
            }      
 
       }
       );
       
       for(int i=0;i<list.size();i++)
       {
    	   //System.out.println(list.get(i).getId()+" "+list.get(i).getGrade());
    	   System.out.println(list.get(i));
       }
}
}

猜你喜欢

转载自blog.csdn.net/Andrelia20171760/article/details/85267424