0

i want to sort the Iterable List by release_date the class Movie has a Date release_date field.

for the leaflet div th:each... so that the newest movies on top and the latest on bottom. i have found the

${#lists.sort(movie)

but this not work for me..

this is the Iterable:

Iterable<Movie> movie = movieDao.findAll();

the movie class:

package com.miyava.movie.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

import org.hibernate.validator.constraints.Length;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.miyava.auditing.AuditedEntity;
import com.miyava.common.NotEmpty;
import com.miyava.genres.model.Genres;

@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id" )
@Entity
public class Movie
    extends AuditedEntity
    implements com.miyava.common.Entity<Long> {

    @Column( name = "movie_id" )
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    @JsonView( DataTablesOutput.View.class )
    private Long id;

    @NotEmpty( message = "movie.messages.title_empty" )
    @Column( nullable = false, unique = false, length = 255 )
    @Length( max = 255, message = "common.message.data_to_long" )
    @JsonView( DataTablesOutput.View.class )
    private String title;

    @NotEmpty( message = "movie.messages.description_empty" )
    @Column( nullable = false, unique = false )
    @Lob
    @JsonView( DataTablesOutput.View.class )
    private String overview;

    @Column( nullable = true, unique = false )
    @JsonView( DataTablesOutput.View.class )
    private String short_Overview;

    @NotEmpty( message = "movie.messages.poster_Path_empty" )
    @Column( nullable = false, unique = false, length = 255 )
    @Length( max = 255, message = "common.message.data_to_long" )
    @JsonView( DataTablesOutput.View.class )
    private String poster_path;

    @NotEmpty( message = "movie.messages.runtime_empty" )
    @Column( nullable = false, unique = false, length = 255 )
    @Length( max = 255, message = "common.message.data_to_long" )
    @JsonView( DataTablesOutput.View.class )
    private String runtime;

    @NotEmpty( message = "movie.messages.status_empty" )
    @Column( nullable = false, unique = false, length = 255 )
    @Length( max = 255, message = "common.message.data_to_long" )
    @JsonView( DataTablesOutput.View.class )
    private String status;

    @Column( unique = false, columnDefinition = "DATETIME", name = "release_date" )
    @JsonView( DataTablesOutput.View.class )
    private Date release_date;

    @ManyToMany( cascade = CascadeType.ALL, targetEntity = Genres.class )
    @JoinTable( name = "movie_genres", joinColumns = @JoinColumn( name = "movie_id", referencedColumnName = "movie_id" ), inverseJoinColumns = @JoinColumn( name = "genres_id", referencedColumnName = "genres_id" ) )
    @JsonView( DataTablesOutput.View.class )
    private List<Genres> genres;

    @OneToMany( mappedBy = "movie" )
    private List<UserMovie> userMovie = new ArrayList<UserMovie>();

    public Movie() {}

    public Movie( String title, String overview, String short_Overview, String status, Date release_date, List<Genres> genres ) {
        super();
        this.title = title;
        this.overview = overview;
        this.short_Overview = short_Overview;
        this.status = status;
        this.release_date = release_date;
        this.genres = genres;
    }

    public Long getId() {
        return id;
    }

    public void setId( Long id ) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle( String title ) {
        this.title = title;
    }

    public String getShortOverview() {
        return short_Overview;
    }

    public void setShortOverview( String short_Overview ) {
        this.short_Overview = short_Overview;
    }

    public String getOverview() {
        return overview;
    }

    public void setOverview( String overview ) {
        this.overview = overview;
    }

    public String getPoster_path() {
        return poster_path;
    }

    public void setPoster_path( String poster_path ) {
        this.poster_path = poster_path;
    }

    public String getRuntime() {
        return runtime;
    }

    public void setRuntime( String runtime ) {
        this.runtime = runtime;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus( String status ) {
        this.status = status;
    }

    public Date getRelease_date() {
        return release_date;
    }

    public void setRelease_date( Date release_date ) {
        this.release_date = release_date;
    }

    public List<Genres> getGenres() {
        return genres;
    }

    public void setGenres( List<Genres> genres ) {
        this.genres = genres;
    }

    public List<UserMovie> getUserMovie() {
        return userMovie;
    }

    public void setUserMovie( List<UserMovie> userMovie ) {
        this.userMovie = userMovie;
    }
}
Damlo
  • 132
  • 1
  • 17
  • To sort on the Java side you can use a `Comparable` as explained in this question. https://stackoverflow.com/questions/5927109/sort-objects-in-arraylist-by-date – yogidilip Jun 01 '17 at 21:02
  • 1
    Comparator helps you on this. If you tried Comparator already please share the code more information.This link help you. https://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property/2784576#2784576 – Janny Jun 01 '17 at 21:03
  • Can you program `findAll()` to return the data in the desiired order (using ORDER BY in the query)? – Kevin Anderson Jun 01 '17 at 23:56

2 Answers2

0

Usually DAO returns List instead of Iterable, but I won't be questioning reason for returning Iterable.

1) This is definitely not a good practice but you can try to do "instanceof" and see it Iterable is actually an instance of List. If it is then cast to List and use Collections.sort on it. Don't forget to pass custom Comparable that compares in movie release_dat.

2) Creat an empty list, load it up from your iterator and then do the sort with you comparator.

tsolakp
  • 5,858
  • 1
  • 22
  • 28
0

If you want to sort by thymeleaf then you have to implement Comparable. Details here

For your case following things to be done to work the ${#lists.sort(movie)

  1. Implement Comparable like public class Movie implements Comparable<Movie>
  2. Override the compareTo method. In your case it would be like following

    @Override public int compareTo(Movie o) { if (release_date.getTime() > o. release_date.getTime()) return 1; else if (release_date.getTime() < o. release_date.getTime()) return -1; else return 0; }

For comparable you can get example here

Zico
  • 2,349
  • 2
  • 22
  • 25