Created
January 19, 2011 21:37
-
-
Save dauger/786916 to your computer and use it in GitHub Desktop.
Extension method to determine if a list is ordered asc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IListOfTExtensions | |
{ | |
public static bool IsOrdered<T, T2>(this IList<T> @this, Func<T, T2> orderedOnProperty) | |
where T2 : IComparable<T2> | |
{ | |
var isLessThanOrEqualTo = true; | |
for (int i = 1; i < @this.Count; i++) | |
{ | |
var comparer = Comparer<T2>.Default; | |
isLessThanOrEqualTo = comparer | |
.Compare(orderedOnProperty(@this[i - 1]), orderedOnProperty(@this[i])) <= 0; | |
if (!isLessThanOrEqualTo) | |
{ | |
break; | |
} | |
} | |
return isLessThanOrEqualTo; | |
} | |
} | |
// usage | |
// var isOrdered = fooList.IsOrdered(x => x.Bar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment