英文:
Collections.sort that accept multiple parameters
问题
我是Java的新手。我正在学习关于Java集合,我对编写一个按属性排序的程序有疑问。所以我有一个带有这些变量的Course类:
public class Course{
private String courseName;
private String courseDescription;
}
还有另一个包含Course类的Student类,Course类是Course变量的链表:
public class Student{
private String name;
private LinkedList<Course> courses;
}
public Student(String name) {
this.name = name;
this.courses = new LinkedList<Course>();
}
我想在Student类中编写一个方法 *public void sortCourse() { }*,该方法应该接受参数,以指定排序是升序还是降序,以及基于哪个课程属性对课程进行排序,并打印排序后的课程列表。我该如何编写这个方法?
英文:
I am new to Java. I am learning about Java Collections and I have question about writing a program to sort by attributes. So I have a class Course with these variables:
public class Course{
private String courseName;
private String courseDescription;
}
And another class Student that contains class Course which is a linked list of Course variables:
public class Student{
private String name;
private LinkedList<Course> courses;
}
public Student(String name) {
this.name = name;
this.courses = new LinkedList<Course>();
}
I want to write a method public void sortCourse() { } in Student class that should accept parameters to specify whether the sorting should be ascending or descending and also based on which course attribute to sort the courses, and print the sorted list of course. How can I write this method?
答案1
得分: 5
正如Fureeish在评论中所说:使其接受一个Comparator
。
像这样:
public void sortCourse(Comparator<Course> comparator) {
this.courses.sort(comparator);
}
调用者将会这样写:
// 按名称升序排序
student.sortCourse(Comparator.comparing(Course::getCourseName));
// 按名称降序排序
student.sortCourse(Comparator.comparing(Course::getCourseName).reversed());
// 先按课程级别升序排序,然后按描述排序
student.sortCourse(Comparator.comparing(Course::getCourseLevel)
.thenComparing(Course::getCourseDescription));
英文:
As Fureeish said in a comment: Make it accept a Comparator
.
Like this:
public void sortCourse(Comparator<Course> comparator) {
this.courses.sort(comparator);
}
The caller would then write something like this:
// Sort ascending by name
student.sortCourse(Comparator.comparing(Course::getCourseName));
// Sort decending by name
student.sortCourse(Comparator.comparing(Course::getCourseName).reversed());
// Sort ascending by course level, then description
student.sortCourse(Comparator.comparing(Course::getCourseLevel)
.thenComparing(Course::getCourseDescription));
答案2
得分: 1
你可以使用Comparable
接口对课程进行排序。为此,你的Course
类需要实现Comparable
接口。
public class Course implements Comparable<Course> {
private String courseName;
private String courseDescription;
public Course(String courseName, String courseDescription){
this.courseName = courseName;
this.courseDescription = courseDescription;
}
public int compareTo(Course c) {
return this.courseName.compareTo(c.courseName);
}
}
现在你可以调用Collections.sort(student.courses)
方法对课程列表进行排序。
public class Student{
private String name;
private LinkedList<Course> courses;
public Student(String name) {
this.name = name;
this.courses = new LinkedList<Course>();
}
public void sortCourse(String sortOrder){
if(sortOrder.equals("asc")){
Collections.sort(this.courses);
} else {
Collections.sort(this.courses);
Collections.reverse(this.courses);
}
}
}
英文:
You can use Comparable
interface to sort the courses. For that your Course
class need to implements the Comparable
interface.
public class Course implements Comparable <Course>{
private String courseName;
private String courseDescription;
public Course(String courseName, String courseDescription){
this.courseName = courseName;
this.courseDescription = courseDescription;
}
public int compareTo(Course c) {
return this.courseName.compareTo(c.courseName);
}
}
Now you can call Collections.sort(student.courses)
method to sort the course list.
public class Student{
private String name;
private LinkedList<Course> courses;
public Student(String name) {
this.name = name;
this.courses = new LinkedList<Course>();
}
public void sortCourse(String sortOrder){
if(sortOrder.equals("asc")){
Collections.sort(this.courses);
} else {
Collections.sort(this.courses);
Collections.reverse(this.courses);
}
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论