The problem here is that it sounds like you want to do an in place sort; in which case, you'd need to know (with a fluent API) when you've finished adding conditions. This contrasts to the LINQ approach, because that uses deferred execution.
For single condition sorts, you can use something like:
public static void Sort<TSource, TValue>(this List<TSource> source,
Func<TSource, TValue> selector) {
var comparer = Comparer<TValue>.Default;
source.Sort((x,y)=>comparer.Compare(selector(x),selector(y)));
}
public static void SortDescending<TSource, TValue>(this List<TSource> source,
Func<TSource, TValue> selector) {
var comparer = Comparer<TValue>.Default;
source.Sort((x,y)=>comparer.Compare(selector(y),selector(x)));
}
It would be harder with multiple sorts; you can't use params
because each TValue
may be different. You'd probably need to either use a terminator step and an intermediary "builder" class (essentially writing a DSL), or you'd need to construct the comparer separately first.