Search code examples
javasortingarraylistalphabetical-sort

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


I wish to sort by name but am unable to figure out how to do so

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);

the problem seems to be with Collections.sort(priority1), I would like it to now sort priority1 alphabetically(same with the other priorities) using name but am unsure on how to do so and nothing I have found works

            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" );
    }

    }

when two things of the same priority are added I receive these set of errors

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)

Solution

  • One of two solutions:

    First one would be to Task to implement Comparator interface. But this bounds sorting order with your model, so it can be troublesome in the future.

    Second would be to pass a Comparator to your sort methods.

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

    Second solution would allow you to change the order in runtime, so it would be more preferable.

    Small improvement:

    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
    

    This way you will avoid unnecessary casting.