我们有一个
SortedList<Resource, Resource> resources =
new SortedList<Resource, Resource>(new ResourceIdle());
我们在模拟中使用的资源清单是这样初始化的,因为我们希望随时传递不同的比较器。我们面临的第一个问题是,SortedList<>
需要在比较器内部进行额外的比较,以便我们可以添加具有相同属性的不同Resource
实例。例如,如果比较器如下所示:
public int Compare(Resource x, Resource y)
{
int priority1 = x.Priority;
int priority2 = y.Priority;
if (priority1 > priority2) {
return -1;
} else if (priority1 < priority2) {
return 1;
} else {
return (x.Id.CompareTo(y.Id));
}
}
如果优先级相同,我们必须进行额外的比较,否则会返回具有相同键的条目异常。因此我的问题是,是否有其他方法可以实现这一点?其次,有没有比 SortedList<>
更快的方式来排序大量对象?