如何将我的特殊订购实施为 CompareTo
所以我有以下 struct
public struct Foo
{
public readonly int FirstLevel;
public readonly int SecondLevel;
public readonly int ThirdLevel;
public readonly int FourthLevel;
}
在某处执行以下操作
var sequence = new Foo[0];
var orderedSequence = sequence
.OrderBy(foo => foo.FirstLevel)
.ThenBy(foo => foo.SecondLevel)
.ThenBy(foo => foo.ThirdLevel)
.ThenBy(foo => foo.FourthLevel);
现在我想实现 System.IComparable
来采取例如。利用 Foo[]
的 .Sort()
的优点。
如何将逻辑(从我的特殊/有线 OrderBy
/ThenBy
)传输到 int CompareTo(Foo foo)
?
So I have following struct
public struct Foo
{
public readonly int FirstLevel;
public readonly int SecondLevel;
public readonly int ThirdLevel;
public readonly int FourthLevel;
}
Somewhere I do the following
var sequence = new Foo[0];
var orderedSequence = sequence
.OrderBy(foo => foo.FirstLevel)
.ThenBy(foo => foo.SecondLevel)
.ThenBy(foo => foo.ThirdLevel)
.ThenBy(foo => foo.FourthLevel);
Now I would like to implement System.IComparable<Foo>
to take eg. advantage of .Sort()
of Foo[]
.
How do I transfer the logic (from my special/wired OrderBy
/ThenBy
) to int CompareTo(Foo foo)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
What about something like: