【发布时间】:2020-03-24 15:35:16
【问题描述】:
场景
我有一个自定义类(称为FeedData),它将收集在HashSet<FeedData> 中
internal struct FeedData
{
internal string ID;
internal Vector2d Location;
}
我现在有一个自定义的IComparer,它可以帮助将HashSet<FeedData> 从最近到最远的位置排序到用户的位置。比较发生在FeedData.Location
internal sealed class DistanceComparer : IComparer<FeedData>
{
private CheapRuler cheapRuler;
private Vector2d userLocation;
//Constructor
internal DistanceComparer(Vector2d _currentLocation)
{
this.userLocation = _currentLocation;
this.cheapRuler = new Geo.CheapRuler(userLocation.x, Geo.CheapRulerUnits.Meters);
}
// Interface IComparer<FeedData> implementation
int IComparer<FeedData>.Compare(FeedData _feedA, FeedData _feedB)
{
int _comparision = cheapRuler.Distance(_feedA.Location, userLocation)
.CompareTo(cheapRuler.Distance(_feedB.Location, userLocation));
return _comparision;
}
}
目标
如果不使用System.Linq,我如何对HashSet<FeedData> 进行排序
使用 HashSet 的原因 与另一个集合(例如 List)相对,是任何时候 FeedData 的计数都可能显着高于性能受到控制的情况。 [HashSet vs. List performance]
HashSet 上的其他操作
- 插入/添加
FeedData - 删除项目
谢谢!
【问题讨论】:
标签: c# collections interface hashset icomparer