无法按特定对象之一对包含多个对象的 ArrayList 进行排序 (java)

Can't sort ArrayList with multiple objects by one of the specific objects (java)

我想按名称排序,但不知道该怎么做

private void displayTasks(List<Task> tasks) {
    List priority1 = new ArrayList();
    List priority2 = new ArrayList();
    List priority3 = new ArrayList();
    List priority4 = new ArrayList();
    List priority5 = new ArrayList();
    for (int i = 0; i < tasks.size(); i++) {
        Task task = (Task)tasks.get(i);
        int priority = task.getPriority();
        String name = task.getName();
        String description = task.getDescription();
        int index = i;
        if (priority == 1){
            priority1.add(task);
            this.taskCollection.updateTask(index, name, description, priority);

问题似乎出在 Collections.sort(priority1) 上,我希望它现在使用名称按字母顺序(与其他优先级相同)对 priority1 进行排序,但我不确定如何这样做,但我没有任何东西找到作品

            Collections.sort(priority1);
        }
        if (priority == 2){
            priority2.add(task);
            this.taskCollection.updateTask(index, name, description, priority);
            Collections.sort(priority2);
        }
        if (priority == 3){
            priority3.add(task);
            this.taskCollection.updateTask(index, name, description, priority);
            Collections.sort(priority3);
        }
        if (priority == 4){
            priority4.add(task);
            this.taskCollection.updateTask(index, name, description, priority);
            Collections.sort(priority4);
        }
        if (priority == 5){
            priority5.add(task);
            this.taskCollection.updateTask(index, name, description, priority);
            Collections.sort(priority5);
        }

    }
    for (int i = 0; i < priority1.size(); i++) {
        Task task = (Task)priority1.get(i);
        System.out.println("Name: " + task.getName()  + ", Description: " + task.getDescription() + ", Priority: 1" );
    }
    for (int i = 0; i < priority2.size(); i++) {
        Task task = (Task)priority2.get(i);
        System.out.println("Name: " + task.getName()  + ", Description: " + task.getDescription() + ", Priority: 2" );
    }
    for (int i = 0; i < priority3.size(); i++) {
        Task task = (Task)priority3.get(i);
        System.out.println("Name: " + task.getName()  + ", Description: " + task.getDescription() + ", Priority: 3" );
    }
    for (int i = 0; i < priority4.size(); i++) {
        Task task = (Task)priority4.get(i);
        System.out.println("Name: " + task.getName()  + ", Description: " + task.getDescription() + ", Priority: 4" );
    }
    for (int i = 0; i < priority5.size(); i++) {
        Task task = (Task)priority5.get(i);
        System.out.println("Name: " + task.getName()  + ", Description: " + task.getDescription() + ", Priority: 5" );
    }

    }

当添加两个优先级相同的东西时,我收到这些错误集

Exception in thread "main" java.lang.ClassCastException: com.greene.project2.Task cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.util.Arrays.sort(Arrays.java:1312)
at java.util.Arrays.sort(Arrays.java:1506)
at java.util.ArrayList.sort(ArrayList.java:1454)
at java.util.Collections.sort(Collections.java:141)
at com.greene.project2.TaskView.displayTasks(Main.java:164)
at com.greene.project2.TaskView.list(Main.java:212)
at com.greene.project2.TaskView.menu(Main.java:247)
at com.greene.project2.TaskView.run(Main.java:257)
at com.greene.project2.Main.main(Main.java:267)

两种解决方案之一:

第一个是任务实现 Comparator 接口。但是这会限制你的模型的排序顺序,所以以后会很麻烦。

其次是将 Comparator 传递给您的排序方法。

Lists.sort(priority4, Comparator.compareBy(Task::getName));

第二种解决方案允许您在运行时更改顺序,因此更可取。

小改进:

private void displayTasks(List<Task> tasks) {
    List<Task> priority1 = new ArrayList<>(); // use generics here
    ...
    for (int i = 0; i < tasks.size(); i++) {
        Task task = tasks.get(i); // casting is redundant, as tasks is a list of Tasks

这样可以避免不必要的转换。

检查代码中的这一行。

Collections.sort(priority5);

java.util.Collections class 中的 sort 方法需要 List 包含实现 Comparable 接口的 class 对象。

因此,定义您的 Task class,使其实现 Comprable 接口。