在Java中,我有一个List,我想对它进行排序。有没有一种方法能为我做到这一点?
排序List<Caution>
我想使用lbEtatCaution
实体:
@Entity
@Table(name="TG_CAUTION")
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Caution implements Serializable {
private static final long serialVersionUID = 1L;
@Transient
public static String NOM_DOSSIER_CAUTION_DEFAULT =" ";
@Id
@SequenceGenerator(name="TG_CAUTION_IDCAUTION_GENERATOR", sequenceName="SID_CAUTION" , allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="TG_CAUTION_IDCAUTION_GENERATOR")
@Column(name="ID_CAUTION")
private long idCaution;
@Column(name="DT_CAUTION")
private Timestamp dtCaution;
@Column(name="DT_ETAT")
private Timestamp dtEtat;
@Column(name="DT_RECEPTION")
private Timestamp dtReception;
@Column(name="MT_CAUTION" ,columnDefinition="number")
private double mtCaution;
@Column(name="RF_DOSSIER")
private String rfDossier;
//bi-directional many-to-one association to TcEtatCaution
@ManyToOne
@JoinColumn(name="CD_ETAT_CAUTION")
private EtatCaution tcEtatCaution;
//bi-directional many-to-one association to TgAgentCaution
@ManyToOne
@JoinColumn(name="ID_AGENT")
private AgentCaution tgAgentCaution;
//bi-directional many-to-one association to TgCaution
@ManyToOne
@JoinColumn(name="ID_CAUTION_REMPLACEE")
private Caution tgCaution;
//bi-directional many-to-one association to TgCaution
@OneToMany(mappedBy="tgCaution")
private List<Caution> tgCautions;
//bi-directional many-to-one association to TgCompteCaution
@ManyToOne
@JoinColumn(name="ID_COMPTE_CAUTION")
private CompteCaution tgCompteCaution;
//bi-directional many-to-one association to TgHistoCaution
@OneToMany(mappedBy="tgCaution")
private List<HistoCaution> tgHistoCautions;
//bi-directional many-to-one association to TgRejet
@OneToMany(mappedBy="tgCaution")
private List<Rejet> tgRejets;
//bi-directional many-to-one association to TgApprehension
//@OneToMany(mappedBy="tgCaution")
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="ID_CAUTION")
private List<Apprehension> tgApprehensions;
//bi-directional many-to-one association to TgBordereau
@OneToMany(mappedBy="tgCaution")
private List<Bordereau> tgBordereaus;
//bi-directional many-to-one association to TgTransfertSirepa
@OneToMany(mappedBy="tgCaution")
private List<TransfertSirepa> tgTransfertSirepas;
public Caution() {
}
//getters and setters
}
实体EtatCaution:
@Entity
@Table(name="TC_ETAT_CAUTION")
public class EtatCaution implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="CD_ETAT_CAUTION")
private long cdEtatCaution;
@Column(name="LB_ETAT_CAUTION")
private String lbEtatCaution;
@Column(name="MN_ETAT_CAUTION" , columnDefinition="char")
private String mnEtatCaution;
public EtatCaution() {
}
//getters and setters
}
答案 0 :(得分:1)
使您的实体类实现Comparable<EtatCaution>
。
public int compareTo(EtatCaution compareObject)
{
return lbEtatCaution.compareTo(compareObject.lbEtatCaution);
}
然后,只需致电Collections.sort(myList)
。
答案 1 :(得分:1)
在相关关系上使用@OrderBy
,例如
//bi-directional many-to-one association to TgCaution
@OneToMany(mappedBy="tgCaution")
@OrderBy("lbEtatCaution ASC")
private List<Caution> tgCautions;
有关详细信息,请参阅http://docs.oracle.com/javaee/6/api/javax/persistence/OrderBy.html
答案 2 :(得分:0)
您需要实现comparator
接口。有一个类似的问题我已经回答了。这会对你有所帮助
这篇文章可以帮助你Java Comparator class to sort particular object array
答案 3 :(得分:0)
从Comparable
界面实现您的实体类
使用
Collection.sort(yourList);