It would be better if you just maintain a List of Task instances and sort that List.
You can use one of the following options to sort a List of Task instances:
- Implementing Comparable Interface
- Using Comparator
Implementing Comparable Interface
To implement Comparable interface, you must override compareTo method in Task class. Since you want to sort Tasks based on date instance field, you can just return the result of date comparison.
Here's how you should override compareTo() method to sort Tasks in ascending order based on date instance field.
@Override
public int compareTo(Task o) {
return this.date.compareTo(o.date);
}
Since Date class already implements Comparable interface, you can just call compareTo method to compare two Date instances.
Now to sort the list of tasks, call sort method of Collections class.
Collections.sort(taskList);
Here's a version of your code that implements Comparable interface and sorts the tasks using date instance field
Using Comparator
There are more than one ways to sort objects using a Comparator interface:
- Create a separate class that implements
Comparator interface
- Use anonymous class or use lambda expression
- Use static methods of Comparator interface
Create a separate class that implements Comparator interface
You can create a class that implements Comparator interface and then override compare function. Implementation of compare function will be same as that of compareTo function implemented above by implementing Comparable interface.
class TaskComparator implements Comparator<Task> {
@Override
public int compare(Task o1, Task o2) {
return o1.getDate().compareTo(o2.getDate());
}
}
To sort the task list, you have two options:
Use sort function of Collections class and pass an instacne of TaskComparator class as a second argument
Collections.sort(taskList, new TaskComparator());
Use sort method of List interface
taskList.sort(new TaskComparator());
Here's a version of your code that creates a separate comparator class to sort the tasks using date instance field
Use anonymous class or use lambda expression
Instead of creating a separate class to implement Comparator interface, you can use an anonymous class
Collections.sort(taskList, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
// code to compare Task objects
}
});
or
taskList.sort(new Comparator<Task>() {
@Override
public int compare(Task o1, Task o2) {
return o1.getDate().compareTo(o2.getDate());
}
});
Java 8 introduced lambda expressions, you can replace anonymous class with lambda expression to make your code concise
Collections.sort(taskList, (o1, o2) -> o1.getDate().compareTo(o2.getDate()));
or
taskList.sort((o1, o2) -> o1.getDate().compareTo(o2.getDate()));
Here's a version of your code that uses lambda expression to implement Comparator interface
Use static methods of Comparator interface
You can also use static method named comparing of Comparator interface. It will return a comparator that will be used for sorting.
Collections.sort(taskList, Comparator.comparing(Task::getDate));
or
taskList.sort(Comparator.comparing(Task::getDate));
Here's a version of your code that uses Comparator.comparing method to sort the tasks using date instance field
For details on how to implement Comparable or Comparator interfaces, see: