我有一个POJO看起来像这样:的CompareTo是传递
public class Pojo implements Comparable<Pojo> {
private String type;
private String journalId;
private Date bookingDate;
private Long account;
private String description;
private BigDecimal debit;
private BigDecimal credit;
....
}
,我想排序这些POJO的列表。目前我compareTo
方法是这样的:
@Override
public int compareTo(EfdisJournal other) {
int i = this.type.compareTo(other.type);
if (i != 0)
return i;
if (this.bookingDate != null && other.bookingDate != null)
i = this.bookingDate.compareTo(other.bookingDate);
if (i != 0)
return i;
if (this.journalId != null && other.journalId != null)
i = this.journalId.compareTo(other.journalId);
if (i != 0)
return i;
return this.account.compareTo(other.account);
}
如果我运行这个compareTo
方法的排序,我得到这个java.lang.IllegalArgumentException: Comparison method violates its general contract
错误。我做了一些谷歌,我认为这是因为一些领域是null
比较。但我不知道如何解决这个问题,或者如果我是正确的,为什么会出现这个错误。
的比较应该像这样工作:1通过type
比较,然后通过bookingDate
比较,为第13由journalId
,最后由account
比较比较。所有的比较应该是递增的。
type
永远不能为nullbookingDate
可以为空journalId
可以为空account
永远不能为null
编辑:
可悲的是我无法实现该方法,因此该命令是根据需要的。然而,我解决了我的问题,因为存储过程产生了2个结果集,其中第二个是需要的顺序,所以我唯一要做的就是使用第二个结果集而不是第一个结果集。
如何你想根据现场是空订购的元素? – Pshemo
您应该决定是否应该使用'null' bookingDate对具有非null bookingDate的_before_或_after_事物进行排序,并适当编写'compareTo'。 (然后'journalId'。)然后你可以得到一个顺序排序的订单。 – khelwood
如果'bookingdate'为null,那么它应该通过'journalId'进行比较,如果它们为null,那么它应该比较'account'。我会更新结果应该如何看起来像 – XtremeBaumer