You can make your object comparable:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    return getDateTime().compareTo(o.getDateTime());
  }
}

And then you sort it by calling:

Collections.sort(myList);

However sometimes you don't want to change your model, like when you want to sort on several different properties. In that case, you can create comparator on the fly:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});

However, the above works only if you're certain that dateTime is not null at the time of comparison. It's wise to handle null as well to avoid NullPointerExceptions:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    if (getDateTime() == null || o.getDateTime() == null)
      return 0;
    return getDateTime().compareTo(o.getDateTime());
  }
}

Or in the second example:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      if (o1.getDateTime() == null || o2.getDateTime() == null)
        return 0;
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});
Answer from Domchi on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java dates › sorting objects in a list by date
Sorting Objects in a List by Date | Baeldung
April 3, 2025 - In this tutorial, we’ll discuss sorting objects in a List by date. Most of the sorting techniques or examples let the user sort a list alphabetically, but in this article, we’ll discuss how to do it with Date objects. We’ll look at using Java’s Comparator class for custom sorting our lists’ values.
Top answer
1 of 15
481

You can make your object comparable:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    return getDateTime().compareTo(o.getDateTime());
  }
}

And then you sort it by calling:

Collections.sort(myList);

However sometimes you don't want to change your model, like when you want to sort on several different properties. In that case, you can create comparator on the fly:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});

However, the above works only if you're certain that dateTime is not null at the time of comparison. It's wise to handle null as well to avoid NullPointerExceptions:

public static class MyObject implements Comparable<MyObject> {

  private Date dateTime;

  public Date getDateTime() {
    return dateTime;
  }

  public void setDateTime(Date datetime) {
    this.dateTime = datetime;
  }

  @Override
  public int compareTo(MyObject o) {
    if (getDateTime() == null || o.getDateTime() == null)
      return 0;
    return getDateTime().compareTo(o.getDateTime());
  }
}

Or in the second example:

Collections.sort(myList, new Comparator<MyObject>() {
  public int compare(MyObject o1, MyObject o2) {
      if (o1.getDateTime() == null || o2.getDateTime() == null)
        return 0;
      return o1.getDateTime().compareTo(o2.getDateTime());
  }
});
2 of 15
119

Since Java 8 the List interface provides the sort method. Combined with lambda expression the easiest solution would be

// sort DateTime typed list
list.sort((d1,d2) -> d1.compareTo(d2));
// or an object which has an DateTime attribute
list.sort((o1,o2) -> o1.getDateTime().compareTo(o2.getDateTime()));
// or like mentioned by Tunaki
list.sort(Comparator.comparing(o -> o.getDateTime()));

Reverse sorting

Java 8 comes also with some handy methods for reverse sorting.

//requested by lily
list.sort(Comparator.comparing(o -> o.getDateTime()).reversed());
Discussions

How to sort list by date using java
I have some files I want to sort the files by their created date ,so how to sort by date by date using java More on sololearn.com
🌐 sololearn.com
2
3
sort by date java basic list - Stack Overflow
I have a list in java that contains lines of string id;city;date;days;price;vehicle. I need to sort all of it by date.(it's from a csv file that I read) My list: List list = new Link... More on stackoverflow.com
🌐 stackoverflow.com
java - Sort data by date - Stack Overflow
I have above type of data in my file and I want to sort it by startDate. What will be the logic? ... Save this answer. ... Show activity on this post. You would need to create class say Data and then implement Comparable in that like: public class Data implements Comparable { private String name; private Date ... More on stackoverflow.com
🌐 stackoverflow.com
[Java] How do I sort an object arraylist by their date fields which are saved as strings?
You would need to implement a custom comparator that takes the string date and converts it to a Date object, and then compares those Dates. You would convert it in this way: SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); Date date = format.parse(stringDate); and then compare the Dates as you normally would. (FWIW, it's far easier to sort dates represented as "yyyy-MM-dd", since dates in that format have a natural lexicographic sort. Not sure if you're able to get the dates in that format, though.) More on reddit.com
🌐 r/learnprogramming
2
1
April 4, 2021
🌐
JavaTechOnline
javatechonline.com › home › how to sort list by date in java 8 ?
How To Sort List By Date In Java 8 ? - Java Sort By Date
July 17, 2026 - In this example, we will sort our ... order to implement sorting a List by LocalDate, Let’s first create a POJO which will have a LocalDate field mandatorily....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-objects-in-arraylist-by-date
Java Program to Sort Objects in ArrayList by Date - GeeksforGeeks
July 23, 2025 - 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. ... // Java Program to Sort Objects in ArrayList by Date // Using Comparator ...
🌐
Baeldung
baeldung.com › home › java › java string › sort date strings in java
Sort Date Strings in Java | Baeldung
January 8, 2024 - The TreeMap class in Java provides automatic sorting of entries based on their keys. By using this feature, we can easily sort date-time strings by creating a TreeMap with key-value pairs of type LocalDateTime and String.
🌐
Coderanch
coderanch.com › t › 381758 › java › sorting-date-java
sorting date in java (Java in General forum at Coderanch)
If you have special needs to sort the list you can also create you own java.util.Comparator and usit in the sort() method to set some orde in the java.util.List. ... I would note that with a YYYY-MM-DD format, sorting the date is equivalent to an alphanumeric sort. That is, the result you get from String's compareTo() method is the same as what you'd get if you converted the Strings to Dates and used Date's compareTo() method. So you can keep things simple here by not using Dates at all.
🌐
Benchresources
benchresources.net › home › java › java 8 – how to sort list by java.util.date in 4 ways ?
Java 8 - How to Sort List by java.util.Date in 4 ways ? - BenchResources.Net
February 2, 2023 - There is a list of Products and we will be sorting product list by java.util.Date in ascending and descending order using Lambda Expression
Find elsewhere
🌐
Code2care
code2care.org › home › java › how to sort a list in java by date
How to Sort a List in Java by Date | Code2care
October 18, 2023 - To sort a list of objects by date in Java, we need to make use of the Collections.sort() method along with a custom Comparator.
🌐
Tpoint Tech
tpointtech.com › sort-dates-in-java
Sort Dates in Java - Tpoint Tech
March 17, 2025 - The sort() method that is a part of Comparator mechanism of Collection class that sorts the data in decreasing order.
🌐
Simplesolution
simplesolution.dev › java-how-to-sort-a-list-of-date
How to Sort a List of Date in Java - Simple Solution
At this first step, create a new Java class named DateUtils, and a new static method named sortDates(List
Top answer
1 of 3
2

You can do this with Comparator interface. I prefer to convert the string into date first.

For dd/MM/yyyy (01/01/1970) pattern example (without conversion):

List<Ticket> list = new LinkedList<Ticket>();
... //fill the list.
Collections.sort(list, new Comparator<Ticket>() {
        public int compare(Ticket t1, Ticket t2) {
            String[] dateParts1 = t1.date.split("/");
            String[] dateParts2 = t2.date.split("/");
            
            int yearResult = dateParts1[2].compareTo(dateParts2[2]);
            if (yearResult != 0) {
                return yearResult;
            }
            
            int monthResult = dateParts1[1].compareTo(dateParts2[1]);
            if (monthResult != 0) {
                return monthResult;
            }
            
            int dayResult = dateParts1[0].compareTo(dateParts2[0]);
            if (dayResult != 0) {
                return dayResult;
            }
            
            return 0;
        }
    });

or (with conversion):

List<Ticket> list = new LinkedList<Ticket>();
... //fill the list.
Collections.sort(list2, new Comparator<Ticket>() {
        public int compare(Ticket t1, Ticket t2) {
            String pattern = "dd/MM/yyyy";
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
            try {
                Date date1 = simpleDateFormat.parse(t1.date);
                Date date2 = simpleDateFormat.parse(t2.date);
                return date1.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return 0;
        }
    });

Also, you can do same thing in class with Comparable interface.

public class Ticket **implements Comparable<Ticket>** {

int id;
String city;
String date;
int days;
float price;
String vehicle;

Ticket(int i, String c, String y, int d, float p, String v) {
    id = i;
    city = c;
    date = y;
    days = d;
    price = p;
    vehicle = v;
}

**@Override
public int compareTo(Ticket o) {
    String[] dateParts1 = this.date.split("/");
    String[] dateParts2 = o.date.split("/");
    int yearResult = dateParts1[2].compareTo(dateParts2[2]);
    if (yearResult != 0) {
        return yearResult;
    }
    int monthResult = dateParts1[1].compareTo(dateParts2[1]);
    if (monthResult != 0) {
        return monthResult;
    }
    int dayResult = dateParts1[0].compareTo(dateParts2[0]);
    if (dayResult != 0) {
        return dayResult;
    }
    return 0;
}**
}

Then:

List<Ticket> list = new LinkedList<Ticket>();
... //fill the list.
Collections.sort(list);
2 of 3
2

You can parse the date String to a date object like LocalDateTime or Instant and etc' (depending on your use case).

I randomly picked LocalDateTime for this example.

Then you can do:

tickets.add(new Ticket(1, "dsa", LocalDateTime.now().plusSeconds(5), 6, 5, "rew"));
tickets.add(new Ticket(0, "dsa", LocalDateTime.now(), 6, 5, "rew"));

List<Ticket> sortedTicketsByDate = tickets.stream()
        .sorted(Comparator.comparing(t -> t.date)) // comparing by date
        .collect(Collectors.toList());

System.out.println(sortedTicketsByDate);

Output:

 [
  Ticket(id=0, city=dsa, date=2021-05-02T23:46:03.214, days=6, price=5.0, vehicle=rew),
  Ticket(id=1, city=dsa, date=2021-05-02T23:46:08.197, days=6, price=5.0, vehicle=rew)
 ]
🌐
Quora
quora.com › How-do-I-sort-an-ArrayList-of-dates-based-on-the-current-date-in-Java
How to sort an ArrayList of dates based on the current date in Java - Quora
Answer (1 of 2): [code]/** This is fully functional method to sort date*/ public static void main(String... args) throws ParseException { ArrayList date=new ArrayList (); SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy"); Date date1=format.parse("02-12-2001"); Date date2=...
🌐
Coderanch
coderanch.com › t › 588925 › java › Sort-Date-date-timestamp-timezone
Sort Date having date, timestamp and timezone. (Java in General forum at Coderanch)
You can use Collections.sort(List<Date>). method. Also look at: Object Ordering. ... jose chiramal wrote:I tried using something like this, but in vain : And the reason is that dates are NOT Strings. It's a common beginner's mistake: I'll make everything a String because I understand what it is.
🌐
YouTube
youtube.com › watch
Quickest & Easiest Way To Sort Date Array In Java - YouTube
Quickest way to sort date-time array in JAVA.Code Link:-https://unpossiblepog.blogspot.com/2020/07/quickest-easiest-way-to-sort-date-array.htmlSubscribe my C...
Published: July 14, 2020
Top answer
1 of 4
1

I assume you have two different classes and both having Date objects. Instead of keeping list of objects I would suggest you creating a base interface like for example:

interface Dated {
   Date getDate();
}

Make your two classes implements this interface:

class A implements Dated {
 int id;
 Date date;

 @Override
 public Date getDate() {
    return date;
 }
}

class B implements Dated {
  int id;
  Date date;

  @Override
  public Date getDate() {
    return date;
  }
}

Create a Comparator like:

class DatedComparator implements Comparator<Dated> {
  @Override
  public int compare(Dated o1, Dated o2) {
    return o1.getDate().compareTo(o2.getDate());
  }
}

Then you can hold a list of Dated objects:

List<Dated> finalList = new ArrayList<Dated>();

which can be easily sorted by using your comparator:

Collections.sort(finalList, new DatedComparator());
2 of 4
1

I assume it's not an option for you to make the different types in your list compatible. But if odering is really your only problem here you could user an adaptive comparator for sorting:

class DateOrder implements Comparator<Object> {
    @Override
    public int compare(Objecto1, Objecto2) {
        return toDate(o1).compareTo(toDate(o2));
    }

    private Date toDate(Object x) {
      if (x instanceof A) {
        return ((A)x).date;
      }
      return ((B)x).date;
    }
}

However having several different types in a single collection is typically not a good idea. So maybe it's better to use an instance adapter (i.e. to wrappers to provide a unique interface to the logic working on that types.) In most cases this approach is more valueable on the longer term as it's not necessary to cast over and over again just to get yet another part of the data. But I don't know enough about your problem to give a clear advice.