Tag: sortedlist icomparable

本机C#支持检查IEnumerable是否已排序?

是否有任何LINQ支持检查IEnumerable是否已排序? 我有一个我要validation的枚举是按非降序排序的,但我似乎无法在C#中找到它的本机支持。 我使用IComparables编写了自己的扩展方法: public static bool IsSorted(this IEnumerable collection) where T : IComparable { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous = enumerator.Current; while (enumerator.MoveNext()) { var current = enumerator.Current; if (previous.CompareTo(current) > 0) return false; previous = current; } } } return true; } 一个使用IComparer对象: public static […]