我想使用 Collections.sort() 方法对包含“计数器”对象的数组列表进行排序。我已经在 Counter 类中实现了类似的接口。我仍然得到一个类转换异常。我已经发布了下面的代码。
public class Counter implements Comparable<Counter>, java.io.Serializable {
private String description;
public Counter() {
}
public Counter(String description) {
this.description = description;
}
public int compareTo(Counter other) {
return description.compareTo(other.description);
}
}
下面的方法是我调用排序方法的地方。
protected List < Counter > prepareCounters(final Integer authorityID) throws ServiceException {
if (authorityID == null) {
return new ArrayList < Counter >();
}
List < Counter > countersInRegistry = new ArrayList < Counter >(counterRegistry.getAllCounters());
List < Counter > countersFromService =
new ArrayList < Counter >(counterService.searchCounters(authorityID, null, null));
countersFromService.removeAll(countersInRegistry);
Collections.sort(countersFromService);
return countersFromService;
}