`Collections.sort` 接受多个参数。

huangapple go评论88阅读模式
英文:

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&lt;Course&gt; courses;
}

public Student(String name) {
        this.name = name;
        this.courses = new LinkedList&lt;Course&gt;();
}

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&lt;Course&gt; 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 &lt;Course&gt;{
    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&lt;Course&gt; courses;

    public Student(String name) {
        this.name = name;
        this.courses = new LinkedList&lt;Course&gt;();
    }

    public void sortCourse(String sortOrder){

        if(sortOrder.equals(&quot;asc&quot;)){
            Collections.sort(this.courses);
        } else {
            Collections.sort(this.courses);
            Collections.reverse(this.courses);
        }
    }
}

</details>



huangapple
  • 本文由 发表于 2020年4月4日 06:38:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/61021454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定