Is the System.ValueTuple sort order officially specified and where?

The new ValueTuple types in C # 7 implement IComparable , but the only documentation that I could find when implementing them just means that the CompareTo return value indicates the relative position in sort order. It does not indicate what the "sort order" actually refers to.

After looking at the source code, I can find that the order is what I would expect - it delegates the comparison of the first default Comparer field, then using the other fields one at a time to break the links, I would rather not depend on this without guaranteeing that he did not consider the implementation detail that could change without violating the specification.

Is this behavior really documented anywhere?

+5
source share
1 answer

According to the source code , CompareTo calls Compare default comparison methods.

  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 client comparator

 int IStructuralComparable.CompareTo(object other, IComparer comparer) 
+6
source

Source: https://habr.com/ru/post/1272715/


All Articles