-8

在 Java 中,我有一个列表,我想对它进行排序。有没有一种方法可以为我做到这一点?

要排序的列表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
}

实体注意事项:

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

4 回答 4

1

让你的实体类实现Comparable<EtatCaution>

public int compareTo(EtatCaution compareObject)
{
    return lbEtatCaution.compareTo(compareObject.lbEtatCaution);
}

然后,只需调用Collections.sort(myList).

于 2013-06-28T14:51:38.470 回答
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

于 2013-06-28T14:56:50.930 回答
0

您需要实现一个comparator接口。我已经回答了一个类似的问题。这将帮助你这篇文章将帮助你Java Comparator 类对特定对象数组进行排序

于 2013-06-28T14:54:52.897 回答
0

Comparable从接口实现您的实体类并使用

Collection.sort(yourList);
于 2013-06-28T15:04:27.173 回答