例如,我有一个名为 RecordGroup 的 java 对象。类签名如下:
公共类 RecordGroup {
private String owner;
private Integer startRow;
private Integer recordCount;
public RecordGroup() {
}
public RecordGroup(String owner, Integer startRow, Integer recordCount) {
this.owner = owner;
this.startRow = startRow;
this.recordCount = recordCount;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public Integer getRecordCount() {
return recordCount;
}
public void setRecordCount(Integer recordCount) {
this.recordCount = recordCount;
}
public Integer getStartRow() {
return startRow;
}
public void setStartRow(Integer startRow) {
this.startRow = startRow;
}
}
而且,我有一个列表,其中包含以下对象的列表。
公共类测试{
List<'RecordGroup'> mergeMap = new ArrayList<'RecordGroup'>();
mergerMap.add(new RecordGroup("RECORD", 1, 6));
mergerMap.add(new RecordGroup("RECORD", 7, 9));
mergerMap.add(new RecordGroup("RECORD", 3, 4));
mergerMap.add(new RecordGroup("ZONE", 3, 1));
mergerMap.add(new RecordGroup("MODULE", 5, 6));
mergerMap.add(new RecordGroup("ZONE", 14, 28));
mergerMap.add(new RecordGroup("ZONE", 6, 30));
mergerMap.add(new RecordGroup("MODULE", 1, 60));
mergerMap.add(new RecordGroup("OFFICE", 2, 4));
mergerMap.add(new RecordGroup("OFFICE", 8, 6));
mergerMap.add(new RecordGroup("USER", 1, 6));
mergerMap.add(new RecordGroup("USER", 9, 8));
mergerMap.add(new RecordGroup("USER", 5, 7));
mergerMap.add(new RecordGroup("OFFICE", 3, 1));
}
我的问题是,如何按它们的“所有者”和“startRow”对上述 RecordGroup 对象列表进行排序,以便它可以按所有者对记录进行分组,即第一个“ZONE”组,然后是“OFFICE”组,然后是“USER”组和然后是“模块”,最后是“记录”组应该出现在列表中。它还应该在排序和分组时考虑“startRow”字段,即按“startRow”字段的值以升序排列每个组。
输出应该是这样的:
mergerMap.add(new RecordGroup("ZONE", 3, 1));
mergerMap.add(new RecordGroup("ZONE", 6, 30));
mergerMap.add(new RecordGroup("ZONE", 14, 28));
mergerMap.add(new RecordGroup("OFFICE", 2, 4));
mergerMap.add(new RecordGroup("OFFICE", 3, 1));
mergerMap.add(new RecordGroup("OFFICE", 8, 6));
mergerMap.add(new RecordGroup("USER", 1, 6));
mergerMap.add(new RecordGroup("USER", 5, 7));
mergerMap.add(new RecordGroup("USER", 9, 8));
mergerMap.add(new RecordGroup("MODULE", 1, 60));
mergerMap.add(new RecordGroup("OFFICE", 2, 4));
mergerMap.add(new RecordGroup("MODULE", 5, 6));
mergerMap.add(new RecordGroup("RECORD", 1, 6));
mergerMap.add(new RecordGroup("RECORD", 3, 4));
mergerMap.add(new RecordGroup("RECORD", 7, 9));