无法通过其中一个特定对象将ArrayList与多个对象排序(java)

时间:2018-04-04 18:34:37

标签: java sorting arraylist alphabetical-sort

我希望按名称排序,但我无法弄清楚如何这样做

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进行排序(与其他优先级相同)使用name,但我不确定如何这样做而且我找不到任何工作

            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)

2 个答案:

答案 0 :(得分:0)

两种解决方案之一:

首先是Task实现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

这样你就可以避免不必要的施法。

答案 1 :(得分:0)

检查代码中的这一行。

Collections.sort(priority5);

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

因此,定义您的Task类,它实现Comprable接口。