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 Program to Sort Objects in ArrayList by Date // Using Comparator interface // Importing required classes import java.util.*;
// Class 1 // helper class for DateItem class DateItem {
// Member variable of this class
String date;
// Constructor of this class
DateItem(String date)
{
// This keyword refers to current object itself
this .date = date;
}
} // Class 2 // Helper class implementing Comparator // from the Comparable interface class sortItems implements Comparator<DateItem> {
// Method of this class
// @Override
public int compare(DateItem a, DateItem b)
{
// Returning the value after comparing the objects
// this will sort the data in Ascending order
return a.date.compareTo(b.date);
}
} // Class 3 // Main class public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating ArrayList class object
// Declaring object of type-DateItem
// class(user-defined)
ArrayList<DateItem> dateList = new ArrayList<>();
// Adding data to the ArrayList
// using standard add() method
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" ));
// Sorting the ArrayList
// using Collections.sort() method
Collections.sort(dateList, new sortItems());
// Display message
System.out.println( "Sorted in Ascending Order" );
// Iterating the list using for-each loop
for (DateItem d : dateList) {
// Printing the sorted items from the List
System.out.println(d.date);
}
}
} |
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 Program to Sort Objects in ArrayList by Date // Using Comparator interface // Importing required classes import java.util.*;
// Class 1 // Helper class class DateItem {
// Member variable
String date;
// Constructor of this class
DateItem(String date)
{
// this keyword refers to current instance itself
this .date = date;
}
} // Class 2 // Helper class implementing Comparable interface class sortItems implements Comparator<DateItem> {
// @Override
// Method of this class
// To compare datetime objects
public int compare(DateItem a, DateItem b)
{
// Returning the value after comparing the objects
// this will sort the data in Descending order
return b.date.compareTo(a.date);
}
} // Class 3 // Main class public class GFG {
// Main driver method
public static void main(String args[])
{
// Creating an ArrayList of DateItem class
// (user-defined)
ArrayList<DateItem> dateList = new ArrayList<>();
// Adding data to the ArrayList
// using standard add() method
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" ));
// Sorting the elements on ArrayList object above
Collections.sort(dateList, new sortItems());
// Display message only
System.out.println( "Sorted in Descending Order" );
// Iterating the List
// using for-each loop
for (DateItem d : dateList) {
// Printing the sorted items from the List
System.out.println(d.date);
}
}
} |
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 Program to Sort Objects in ArrayList by Date // Using Collections.sort() method // Importing required classes import java.util.*;
// Main class public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList of String to
// store the Dates
ArrayList<String> datesList = new ArrayList<>();
// Adding date to ArrayList
// using standard add() method
datesList.add( "2020-03-25" );
datesList.add( "2019-01-27" );
datesList.add( "2020-03-26" );
datesList.add( "2020-02-26" );
// Display message only
System.out.println(
"Dates Object before sorting : " );
// Iterating in the ArrayList
// using for each loop
for (String dates : datesList) {
// Printing the data from the list
System.out.println(dates);
}
// Sorting the ArrayList
// using Collections.sort() method
Collections.sort(datesList);
// Display message only
System.out.println( "Dates Object after sorting : " );
// Iterating in the ArrayList
// using for-each loop
for (String dates : datesList) {
// Printing the data from the list
System.out.println(dates);
}
}
} |
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