javacodex.com
Java Examples
Java Examples
memu home questions

Sorting A List Of Objects By Date

The following Java example demonstrates how to sort a List of Objects by a date property.

The following Comparator is implemented:

Collections.sort(list, new Comparator() {
   public int compare(Person p1, Person p2) {
      return Long.valueOf(p1.getBirthdate().getTime()).compareTo(p2.getBirthdate().getTime());
   }
});

Source: (Person.java)

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
 
public class Person {
 
    private String name;
    private Date birthdate;
 
    public Person(String name, Date birthdate) {
        this.name = name;
        this.birthdate = birthdate;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Date getBirthdate() {
        return birthdate;
    }
 
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }
 
    public static void main(String[] args) throws ParseException {
        ArrayList<Person> list = new ArrayList<>();
        list.add(new Person("John", new SimpleDateFormat("dd-MM-yyyy").parse("01-01-2010")));
        list.add(new Person("Tom", new SimpleDateFormat("dd-MM-yyyy").parse("01-01-1990")));
        list.add(new Person("Bob", new SimpleDateFormat("dd-MM-yyyy").parse("01-01-2000")));
 
        System.out.println("Before Sort");
        for (Person person : list) {
            System.out.println(person.getName() + " " + person.getBirthdate());
        }
 
        // Sort in assending order
        Collections.sort(list, new Comparator<Person>() {
            public int compare(Person p1, Person p2) {
                return Long.valueOf(p1.getBirthdate().getTime()).compareTo(p2.getBirthdate().getTime());
            }
        });
 
        System.out.println("After Assending sort");
        for(Person person: list){
            System.out.println(person.getName() + " " + person.getBirthdate());
        }
 
        // Sort in dessending order
        Collections.sort(list, new Comparator<Person>() {
            public int compare(Person p1, Person p2) {
                return Long.valueOf(p2.getBirthdate().getTime()).compareTo(p1.getBirthdate().getTime());
            }
        });
 
        System.out.println("After Descending sort");
        for(Person person: list){
            System.out.println(person.getName() + " " + person.getBirthdate());
        }
    }
}
 

Output:

$ java Person
Before Sort
John Fri Jan 01 00:00:00 EST 2010
Tom Mon Jan 01 00:00:00 EST 1990
Bob Sat Jan 01 00:00:00 EST 2000
After Assending sort
Tom Mon Jan 01 00:00:00 EST 1990
Bob Sat Jan 01 00:00:00 EST 2000
John Fri Jan 01 00:00:00 EST 2010
After Descending sort
John Fri Jan 01 00:00:00 EST 2010
Bob Sat Jan 01 00:00:00 EST 2000
Tom Mon Jan 01 00:00:00 EST 1990

Contact: javacodex@yahoo.com