You can transfer the signature of the OrderBy extension method:
Update 1 , you must be explicit in the first generic parameter for your Selector Func key. I'm going to guess your type and name it "Song".
public void Sort<TKey>(SortOrder sortOrder, Func<Song, TKey> keySelector) { if (sortOrder == SortOrder.Descending) { _list = _list.OrderByDescending(keySelector).ToList(); } else { _list = _list.OrderBy(keySelector).ToList(); } }
Now you can call "Sort" as follows:
Sort(SortOrder.Descending, x => x.Album);
Update 2
Comment on Tom Lokhorst: If you want to predefine some sorting criteria, you can do this by specifying a class as follows:
public static class SortColumn { public static readonly Func<Song, string> Artist = x => x.Artist; public static readonly Func<Song, string> Album = x => x.Album; }
Now you can just call:
Sort(SortOrder.Descending, SortColumn.Artist);
Matt hamilton
source share