Java Program to Sort Objects in ArrayList by Date
Last Updated :
21 Feb, 2022
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 do with Comparator. Both approaches are discussed below where the object is also created of the user-defined type.
Methods:
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.
Approach:
- 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.
Example 1
Java
import java.util.*;
class DateItem {
String date;
DateItem(String date)
{
this .date = date;
}
}
class sortItems implements Comparator<DateItem> {
public int compare(DateItem a, DateItem b)
{
return a.date.compareTo(b.date);
}
}
public class GFG {
public static void main(String args[])
{
ArrayList<DateItem> dateList = new ArrayList<>();
dateList.add( new DateItem( "2020-03-25" ));
dateList.add( new DateItem( "2019-01-27" ));
dateList.add( new DateItem( "1998-01-27" ));
dateList.add( new DateItem( "1998-02-26" ));
Collections.sort(dateList, new sortItems());
System.out.println( "Sorted in Ascending Order" );
for (DateItem d : dateList) {
System.out.println(d.date);
}
}
}
|
Output
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:
Example 2
Java
import java.util.*;
class DateItem {
String date;
DateItem(String date)
{
this .date = date;
}
}
class sortItems implements Comparator<DateItem> {
public int compare(DateItem a, DateItem b)
{
return b.date.compareTo(a.date);
}
}
public class GFG {
public static void main(String args[])
{
ArrayList<DateItem> dateList = new ArrayList<>();
dateList.add( new DateItem( "2020-03-25" ));
dateList.add( new DateItem( "2019-01-27" ));
dateList.add( new DateItem( "1998-01-27" ));
dateList.add( new DateItem( "1998-02-26" ));
Collections.sort(dateList, new sortItems());
System.out.println( "Sorted in Descending Order" );
for (DateItem d : dateList) {
System.out.println(d.date);
}
}
}
|
Output
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.
Example
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> datesList = new ArrayList<>();
datesList.add( "2020-03-25" );
datesList.add( "2019-01-27" );
datesList.add( "2020-03-26" );
datesList.add( "2020-02-26" );
System.out.println(
"Dates Object before sorting : " );
for (String dates : datesList) {
System.out.println(dates);
}
Collections.sort(datesList);
System.out.println( "Dates Object after sorting : " );
for (String dates : datesList) {
System.out.println(dates);
}
}
}
|
Output
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...