我正在考虑用 SortedSet 替换 HashSet,因为它更适合我存储的数据。
但是,到目前为止我看到的所有示例都与存储简单对象有关 - int、字符串等。
我想为具有许多属性的自定义类实现此功能,但是该类还包含一个我想用作“索引器”的日期。
问题是我如何着手为要使用的集合声明一个自定义索引器,它将覆盖默认行为?
提前致谢。
最佳答案
实现IComparer并将其传递给SortedSet构造函数;
参见:
https://msdn.microsoft.com/en-us/library/dd395024%28v=vs.110%29.aspx
例如:我用这个
internal class SortedIndex
{
public double Comparable { get; set; }
public int Index { get; set; }
}
internal class SortedIndexComparar : IComparer<SortedIndex>
{
public int Compare(SortedIndex x, SortedIndex y)
{
return x.Comparable.CompareTo(y.Comparable);
}
}
关于c# - SortedSet - 存储类对象时的自定义顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28067196/