好的,我有很多方法,如下所示: - 按艺术家,专辑,年份等对列表进行排序.
public void SortByAlbum(SortOrder sortOrder) { if (sortOrder == SortOrder.Ascending) _list = _list.OrderBy(x => x.Album).ToList(); else if (sortOrder == SortOrder.Descending) _list = _list.OrderByDescending(x => x.Album).ToList(); }
还有这个:
public void SortByArtist(SortOrder sortOrder) { if (sortOrder == SortOrder.Ascending) _list = _list.OrderBy(x => x.Artist).ToList(); else if (sortOrder == SortOrder.Descending) _list = _list.OrderByDescending(x => x.Artist).ToList(); }
现在显然这不是好代码所以它需要重构为一个Sort()方法,但我不知道如何以最简单的方式做到这一点.我不在乎它是否使用IComparer或LINQ.
我希望它看起来像这样:
public void Sort(SortOrder sortOrder, SortType sortType) { //implementation here } public enum SortType { Artist, Album, Year }
那么最简洁的方法是什么,没有代码重复?
谢谢,李
您应该能够模仿OrderBy扩展方法的签名:
更新1您必须在keySelector Func的第一个通用参数中显式.我打算猜你的类型并称之为"歌曲".
public void Sort(SortOrder sortOrder, Func keySelector) { if (sortOrder == SortOrder.Descending) { _list = _list.OrderByDescending(keySelector).ToList(); } else { _list = _list.OrderBy(keySelector).ToList(); } }
现在您可以像这样调用"排序":
Sort(SortOrder.Descending, x => x.Album);
更新2
跟进Tom Lokhorst的评论:如果你想预定义一些速记排序标准,你可以通过定义这样一个类来实现:
public static class SortColumn { public static readonly FuncArtist = x => x.Artist; public static readonly Func Album = x => x.Album; }
现在你可以简单地打电话:
Sort(SortOrder.Descending, SortColumn.Artist);