Java Program to Sort Objects in ArrayList by Date
The foremost tool that strikes is the sort() method to be used for the comparator mechanism of the Collections class which sorts in the decreasing order. Yes if in generic we want to achieve the goal considering the boundary condition where objects to sorted are user-defined then blindly fo with Comparator. Both approaches are discussed below where the object is also created of the user-defined type.
In Java, we have multiple methods to sort objects in ArrayList by Date. This can be done by using the Comparable<> interface or by the Collections.sort() method, to accomplish this task you can use any one of them.
- Using Comparator interface
- Using Collections.sort() method
Now let’s discuss all of them one by one.
Method 1: Using Comparator interface
The Java Comparator interface is used to order the objects of the user-defined class. By using the Comparator<> interface you can sort the elements on the basis of any data member defined in the user-defined class. The java.util package contains this interface. We can do this task by using methods compare() and compareTo() which will be used for comparing the objects of our DateItem class.
- Create a new class and name that class as DateItem and create a variable of type String, then create a constructor of class DateItem and pass that String type variable here.
- In the main method create an ArrayList of type DateItem.
- Store the objects of DateItem in the ArrayList.
- Create another class called sortItems which implements Comparator and pass our DateItem class to a comparator.
- Now in the Comparator class create a compare method that returns an integer and takes two parameters of the ‘DateItem‘ object as compare(Object obj1, Object obj2).
- Inside the compare method for return value use the compareTo() method which will return the specified value by comparing the DateItem objects.
- Now in the main method use Collections.sort() method and pass the ArrayList and ‘SortItem‘ class object to it, it will sort the dates, and output will be generated.
Sorted in Ascending Order 1998-01-27 1998-02-26 2019-01-27 2020-03-25
Note : This code will sort the dates in Ascending order. If you want to change the order of sorting you can refer to the program below:
Sorted in Descending Order 2020-03-25 2019-01-27 1998-02-26 1998-01-27
Method 2: Using Collections.sort() method
The Collections.sort() method can be used to sort the ArrayList of custom objects. We can use this method to sort the Objects in ArrayList by the Date. java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collections in ascending order. It works similar to java.util.Arrays.sort() method, but it is better than it as it can sort the elements of Array as well as a linked list, queue, and many more presents in it.
Dates Object before sorting : 2020-03-25 2019-01-27 2020-03-26 2020-02-26 Dates Object after sorting : 2019-01-27 2020-02-26 2020-03-25 2020-03-26
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.