【问题标题】:Sort Iterable List variable排序可迭代列表变量
【发布时间】:2017-11-03 03:50:47
【问题描述】:

我想按 release_date 对 Iterable List 进行排序 Movie 类有一个 Date release_date 字段。

对于传单 div th:each... 以便最新电影在顶部,最新电影在底部。 我找到了

${#lists.sort(movie)

但这对我不起作用..

这是可迭代的:

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

电影类:

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;
    }
}

【问题讨论】:

  • 要在 Java 端排序,您可以使用Comparable,如本问题所述。 stackoverflow.com/questions/5927109/…
  • Comparator 帮助你解决这个问题。如果您已经尝试过 Comparator,请分享代码更多信息。此链接对您有帮助。 stackoverflow.com/questions/2784514/…
  • 您可以对findAll() 进行编程以按所需顺序返回数据(在查询中使用 ORDER BY)吗?

标签: java spring-mvc spring-boot thymeleaf iterable


【解决方案1】:

如果你想按百里香排序,那么你必须实现Comparable。详情here

对于您的情况,请关注${#lists.sort(movie)

  1. public class Movie implements Comparable&lt;Movie&gt;一样实现Comparable
  2. 覆盖compareTo 方法。在你的情况下,它就像以下

    @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; }

对于comparable,您可以获得示例here

【讨论】:

    【解决方案2】:

    通常 DAO 返回 List 而不是 Iterable,但我不会质疑返回 Iterable 的原因。

    1) 这绝对不是一个好习惯,但是您可以尝试执行“instanceof”并看到 Iterable 实际上是 List 的一个实例。如果然后将其转换为 List 并在其上使用 Collections.sort 。不要忘记传递在电影 release_dat 中比较的自定义 Comparable。

    2) 创建一个空列表,从迭代器中加载它,然后使用比较器进行排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      • 2023-04-08
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      相关资源
      最近更新 更多