例如,我有一个名为RecordGroup的java对象。类签名如下:
public class 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,其中包含上面提到的上述对象的列表。
公共课测试{
列表与LT; 'RecordGroup' > mergerMap = 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));
}
我的问题是,如何按照'owner'和'startRow'对RecordGroup对象的上述列表进行排序,以便它可以按所有者分组记录,即首先是“ZONE”组,然后是“OFFICE”组,然后是“USER” “组,然后”MODULE“,最后”RECORD“组应出现在列表中。它还应该在排序和分组时考虑“startRow”字段,即按“startRow”字段的值按升序排列每个组。
Out put应该是这样的:
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));
答案 0 :(得分:0)
答案 1 :(得分:0)
简单实现Java的Comparable
接口,根据需要覆盖compareTo
函数,并将RecordGroups推送到SortedSet,如TreeSet
或某事。
http://docs.oracle.com/javase/6/docs/api/java/lang/Comparable.html
http://docs.oracle.com/javase/6/docs/api/java/util/TreeSet.html
更新:如果您需要为不同的位置进行不同的排序,则实体绑定的比较功能将会出现。不是你想要实现的。 (整个系统将是平等的)。如果是这种情况,只需在每个排序情况下使用一个Comperator。
答案 2 :(得分:0)
您应该实现java.lang.Comparable界面:
public class RecordGroup implements Comparable<RecordGroup> {
//Rest of your implementation
@Override
public int compareTo(RecordGroup o) {
//logic to compare two RecordGroup objects
}
}
答案 3 :(得分:0)
比较者为您Comparator
执行此操作使用以下代码。
public int compare(RecordGroup o1, RecordGroup o2) {
if (o1.getOwner().compareTo(o2.getOwner()) == 0) {
return o1.getStartRow() - o2.getStartRow();
} else {
return o1.getOwner().compareTo(o2.getOwner());
}
}
});
检查工作演示转到http://ideone.com/rsM9Un进行演示这里startRow是升序
输出:
[MODULE , 1
, MODULE , 5
, OFFICE , 2
, OFFICE , 3
, OFFICE , 8
, RECORD , 1
, RECORD , 3
, RECORD , 7
, USER , 1
, USER , 5
, USER , 9
, ZONE , 3
, ZONE , 6
, ZONE , 14
]