[英]Is the sort order of System.ValueTuple officially specified, and where?
The new ValueTuple
types in C# 7 implement IComparable
, but the only documentation I have been able to find on their implementation of this simply states that CompareTo
's return value indicates relative position "in the sort order". C#7中新的ValueTuple
类型实现了IComparable
,但我能够在其实现中找到的唯一文档只是说明CompareTo
的返回值表示“在排序顺序中”的相对位置。 It does not state what the "sort order" referred to actually is. 它没有说明实际上所指的“排序顺序”是什么。
By examining the source, I can find that the order is what I would expect - it delegates to comparing the first field by its default Comparer
, then using the other fields one at a time, in order, to break ties. 通过检查源代码,我可以发现订单是我期望的 - 它委托比较第一个字段的默认Comparer
,然后一次一个地使用其他字段,以便打破关系。 I would prefer not to depend on this without a guarantee that it's not considered an implementation detail that could change without violating specification, however. 我宁愿不依赖于此而不保证它不被认为是可以在不违反规范的情况下改变的实现细节。
Is this behavior actually documented anywhere? 这种行为是否实际记录在任
According to the source code , CompareTo
calls the Compare
methods of the default comparers 根据源代码 , CompareTo
调用默认比较器的Compare
方法
public int CompareTo(ValueTuple<T1, T2, T3> other)
{
int c = Comparer<T1>.Default.Compare(Item1, other.Item1);
if (c != 0) return c;
c = Comparer<T2>.Default.Compare(Item2, other.Item2);
if (c != 0) return c;
return Comparer<T3>.Default.Compare(Item3, other.Item3);
}
but you can explicitly provide a customer comparer 但您可以明确提供客户比较器
int IStructuralComparable.CompareTo(object other, IComparer comparer)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.