有两个索引的有序队列

Cap*_*mic 1 c# queue data-structures

我需要一个有序队列,其中对象将按主要和次要值排序.

class Object
{
  int PrimaryValue;
  int SecondaryValue;
}
Run Code Online (Sandbox Code Playgroud)

队列中Object的位置必须由PrimaryValue确定.具有较高PrimaryValue的对象必须位于具有较低PrimaryValue的对象之前.但是,对于具有相同PrimaryValue的两个对象,必须使用SecondaryValue来确定优先级.此外,我需要两个函数来获得将返回相应迭代器的迭代器GetFirst()和后向迭代GetLast()器.

Mar*_*tos 7

class Obj : IComparable<Obj>
{
    int PrimaryValue;
    int SecondaryValue;

    public int CompareTo(Obj other)
    {
        if (other == null) throw new ArgumentNullException("other");
        int diff = PrimaryValue - other.PrimaryValue;
        return diff != 0 ? diff : SecondaryValue - other.SecondaryValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不太确定前向和反向迭代器是什么意思,对于C#中不存在的概念,这是C++术语.您只需使用foreach (var e in coll) ...,然后使用System.Linq,就可以在正向迭代集合:foreach (var e in coll.Reverse()) ....