Collections.sort(list, new Comparator() { public int compare(Person p1, Person p2) { return Long.valueOf(p1.getBirthdate().getTime()).compareTo(p2.getBirthdate().getTime()); } });
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()); } } }
$ 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
Java sorting objects in ArrayList by date? Order a collection by a Date How to sort a List of objects by their date? Collections.sort by a date property on the objects in a list Order array of objects by date property
Add Contents Of One ArrayList to Another
Array Of ArrayLists
ArrayList Adding Elements
ArrayList Remove Elements
ArrayList Remove Elements By Name
Basic HashMap Example
Basic HashSet Example
Basic TreeSet Example
Binary Search On Array
Calculate The Intersection Of Two Sets
Clone An ArrayList
Cosequential Processing
Count Duplicated Items In A List
Count Unique Characters In A String
Double Ended Queue
Find Minimum and Maximum Element in a Set
Finding The Longest String In A String Array
Initialize An ArrayList
Iterate Through A HashMap
Iterator Example
ListIterator Example
Parallel Arrays
Populate List With Comma Seperated Sting
PriorityQueue Sorted By String Length
Read Elements From A HashSet Using An Iterator
Read Elements From An ArrayList Using An Iterator
Simple Way To Print The Contents Of An Array
Sort An ArrayList Using A Comparator
Sorting A HashMap Using A TreeMap
Sorting A List Of Objects By Date
Stack Example
Swap Two Elements In An ArrayList