How to sort a List of objects by their date (java collections, List<Object>)

java sort list of objects by field
java-stream sort by date descending
sort list of list java
sort list java
sort list of objects in java without comparator
sort list based on another list java
collections.sort java 8
java collections sort comparator
private List<Movie> movieItems = null;
public List<Movie> getMovieItems() {
    final int first = 0;
    if (movieItems == null) {
        movieItems = jpaController.findRange(new int[]{pagingInfo.getFirstItem(), pagingInfo.getFirstItem() + pagingInfo.getBatchSize()});
        Collections.sort(movieItems, new Comparator(){
           public int compare (Object o1, Object o2){
               Date d1 = movieItems.get(((Movie)o1).getMovieId()).getDate();
               Date d2 = movieItems.get(((Movie)o2).getMovieId()).getDate();
                   movieItems.set(1, (Movie)o1);
                   movieItems.set(2, (Movie)o2);
               return first;
    return movieItems;

jpaController is bringing back 4 movies and is giving me the following

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 4 at java.util.Vector.get( at entitybeans.jsf.PeliculaController$ at java.util.Arrays.mergeSort( at java.util.Arrays.sort( at java.util.Collections.sort( at entitybeans.jsf.PeliculaController.getPeliculaItems( at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( at sun.reflect.DelegatingMethodAccessorImpl.invoke( at java.lang.reflect.Method.invoke( at javax.el.BeanELResolver.getValue( at javax.el.CompositeELResolver.getValue( at com.sun.faces.el.FacesCompositeELResolver.getValue( at com.sun.el.parser.AstValue.getValue( at com.sun.el.parser.AstValue.getValue(

In your compare method, o1 and o2 are already elements in the movieItems list. So, you should do something like this:

Collections.sort(movieItems, new Comparator<Movie>() {
    public int compare(Movie m1, Movie m2) {
        return m1.getDate().compareTo(m2.getDate());

Java 8 Lambda Comparator example for Sorting List of Custom , How do you sort a list object in Java 8? Learn to use Collections.sort() method to sort arraylist of custom objects in java with examples. By default, this method sorts the unsorted list into ascending order, according to the natural ordering of its elements. We can use Collections.reverseOrder() method for reverse sorting. 1. Sort ArrayList of objects – Collections.sort( list )

Do not access or modify the collection in the Comparator. The comparator should be used only to determine which object is comes before another. The two objects that are to be compared are supplied as arguments.

Date itself is comparable, so, using generics:

class MovieComparator implements Comparator<Movie> {
    public int compare(Movie m1, Movie m2) {
       //possibly check for nulls to avoid NullPointerException
       return m1.getDate().compareTo(m2.getDate());

And do not instantiate the comparator on each sort. Use:

private static final MovieComparator comparator = new MovieComparator();

Java Collections sort(), Collections.sort(myList, new Comparator<MyObject>() { public int compare(​MyObject o1, MyObject Since Java 8 the List interface provides the sort method​. Sort a List of Objects by Field. So we have a list of users and want to sort them by createdDate. Let's cover several different approaches and explore the benefits and limitations of each. Option 1: Collections.sort() with Comparable. We can use the Comparable interface to define the default sort criteria for a class.

In Java 8, it's now as simple as:


Sort objects in ArrayList by date?, reverseOrder() method for reverse sorting. 1. Sort ArrayList of objects – Collections.sort( list ). It sorts the specified list into ascending order or their natural  How To Sort Objects In Java Any Java developer worth their salt has used the Collection framework. Here is a simple tutorial that explains how to sort in Java.

You're using Comparators incorrectly.

 Collections.sort(movieItems, new Comparator<Movie>(){
           public int compare (Movie m1, Movie m2){
               return m1.getDate().compareTo(m2.getDate());

Java 8 - Collections sort() method, In Java, it's pretty easy to sort elements in a list collection using the Collections. sort() static utility method. This method has two forms as follows: void sort(List<T> list): sorts the list into ascending order according to its elements' natural ordering. 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 Collection in ascending order. It works similar to java.util.Arrays.sort () method but it is better then as it can sort the elements of Array as well as linked list, queue and many more present in it.

I'd add Commons NullComparator instead to avoid some problems...

Java Sort List Collections Examples, These sorting can be done based on the date or any other condition. For example, making object comparable<> , Collections.sort() and list.sort()  We generally use Collections.sort() method to sort a simple array list. However if the ArrayList is of custom object type then in such case you have two options for sorting- comparable and comparator interfaces.

How to Sort Objects in ArrayList by date in Java, Collections.sort(); Java 8 List.sort(); Java 8 Stream.sorted() Then we create an ArrayList of Date objects and add items to it. For Descending order, we use Collections.reverse() on the sorted ArrayList by Ascending: 2. ArrayList sort() – Sort list of objects by field. Comparators are most useful when we want to sort a given list of objects – but not in natural order. For example, a list of employees should be sorted on their employee id, naturally. Another usecase of comparator is that when objects in the list are not implementing Comparable interface

Java - Sort ArrayList of Objects, We generally use Collections.sort() method to sort a simple array list. However if the ArrayList is of custom object type then in such case you have two options for  Im trying to sort through an arraylist of objects by a particular value within the object. What would be the best approach to do such a thing. Should I use Collections.sort() with some kind of comp

Java ArrayList of Object Sort Example (Comparable And Comparator), In this tutorial we will see how to sort a list of custom objects using lambda expression in Java. Before we see the complete example, lets see what is the  Here is a version of Robert B's answer that works for List<T> and sorting by a specified String property of the object using Reflection and no 3rd party libraries