Java 可排序列表变量
我想按发布日期对列表进行排序 类电影有一个日期发布\日期字段 对于传单部门:每个。。。所以最新的电影在顶部,最新的在底部。 我已经找到了答案Java 可排序列表变量,java,spring-mvc,spring-boot,thymeleaf,iterable,Java,Spring Mvc,Spring Boot,Thymeleaf,Iterable,我想按发布日期对列表进行排序 类电影有一个日期发布\日期字段 对于传单部门:每个。。。所以最新的电影在顶部,最新的在底部。 我已经找到了答案 ${#lists.sort(movie) 但这对我不起作用 这是最重要的: Iterable<Movie> movie = movieDao.findAll(); Iterable movie=movieDao.findAll(); 电影课: package com.miyava.movie.model; import java.uti
${#lists.sort(movie)
但这对我不起作用
这是最重要的:
Iterable<Movie> movie = movieDao.findAll();
Iterable 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;
}
}
package com.miyava.movie.model;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入javax.persistence.*;
导入org.hibernate.validator.constraints.Length;
导入org.springframework.data.jpa.datatables.mapping.datatables输出;
导入com.fasterxml.jackson.annotation.JsonIdentityInfo;
导入com.fasterxml.jackson.annotation.JsonView;
导入com.fasterxml.jackson.annotation.ObjectiveGenerators;
导入com.miyava.auditing.AuditedEntity;
import com.miyava.common.NotEmpty;
导入com.miyava.genres.model.genres;
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
@实体
公映
扩大审计范围
实现com.miyava.common.Entity{
@列(name=“movie\u id”)
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@JsonView(DataTablesOutput.View.class)
私人长id;
@NotEmpty(message=“movie.messages.title\u empty”)
@列(null=false,unique=false,length=255)
@长度(最大值=255,message=“common.message.data”到“long”)
@JsonView(DataTablesOutput.View.class)
私有字符串标题;
@NotEmpty(message=“movie.messages.description\u empty”)
@列(nullable=false,unique=false)
@高球
@JsonView(DataTablesOutput.View.class)
私有字符串概述;
@列(nullable=true,unique=false)
@JsonView(DataTablesOutput.View.class)
私有字符串短_概述;
@NotEmpty(message=“movie.messages.poster\u Path\u empty”)
@列(null=false,unique=false,length=255)
@长度(最大值=255,message=“common.message.data”到“long”)
@JsonView(DataTablesOutput.View.class)
私家车路线;;
@NotEmpty(message=“movie.messages.runtime\u empty”)
@列(null=false,unique=false,length=255)
@长度(最大值=255,message=“common.message.data”到“long”)
@JsonView(DataTablesOutput.View.class)
私有字符串运行时;
@NotEmpty(message=“movie.messages.status\u empty”)
@列(null=false,unique=false,length=255)
@长度(最大值=255,message=“common.message.data”到“long”)
@JsonView(DataTablesOutput.View.class)
私有字符串状态;
@列(unique=false,columnDefinition=“DATETIME”,name=“release\u date”)
@JsonView(DataTablesOutput.View.class)
私人日期发布日期;
@ManyToMany(cascade=CascadeType.ALL,targetEntity=Genres.class)
@JoinTable(name=“movie\u genres”,joinColumns=@JoinColumn(name=“movie\u id”,referencedColumnName=“movie\u id”),inverseJoinColumns=@JoinColumn(name=“genres\u id”,referencedColumnName=“genres\u id”))
@JsonView(DataTablesOutput.View.class)
私人列表体裁;
@OneToMany(mappedBy=“电影”)
private List userMovie=new ArrayList();
公共电影(){}
公共电影(字符串标题、字符串概述、字符串简短概述、字符串状态、发布日期、列表类型){
超级();
this.title=标题;
this.overview=概述;
this.short\u Overview=short\u Overview;
这个状态=状态;
this.release\u date=发布日期;
this.genres=流派;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getShortOverview(){
返回简短概述;
}
公共void setShortOverview(字符串短\u概述){
this.short\u Overview=short\u Overview;
}
公共字符串getOverview(){
退货概述;
}
公共void集合概述(字符串概述){
this.overview=概述;
}
公共字符串getPoster_path(){
回程;;
}
公共无效设置海报路径(字符串海报路径){
this.poster\u path=poster\u path;
}
公共字符串getRuntime(){
返回运行时;
}
公共void setRuntime(字符串运行时){
this.runtime=运行时;
}
公共字符串getStatus(){
返回状态;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公开日期getRelease_Date(){
返回发布日期;
}
公共作废设置发布日期(发布日期){
this.release\u date=发布日期;
}
公共列表getGenres(){
回归体裁;
}
公共类型(列表类型){
this.genres=流派;
}
公共列表getUserMovie(){
返回用户电影;
}
public void setUserMovie(列出userMovie){
this.userMovie=userMovie;
}
}
通常DAO返回列表而不是Iterable,但我不会质疑返回Iterable的原因
1) 这绝对不是一个好的实践,但您可以尝试执行“instanceof”,并看到Iterable实际上是List的一个实例。如果然后将其强制转换为列表并对其使用Collections.sort。别忘了通过电影发行版中比较的定制比较
2) 创建一个空列表,从迭代器加载它,然后使用comparator进行排序。如果要按thymeleaf排序,则必须实现
Comparable
。细节
对于您的案例,请执行以下操作来处理${#lists.sort(movie)
Comparable
类似公共类电影实现Comparable
compareTo
方法。在您的情况下