好的,我有很多方法,如下所示: - 按照艺术家,专辑,年份等对列表进行排序。
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
}
那么最简单的方法是什么,没有代码重复?
谢谢,李
答案 0 :(得分:12)
您应该能够模仿OrderBy扩展方法的签名:
更新1 您必须在keySelector Func的第一个通用参数中显式。我打算猜你的类型并称之为“歌曲”。
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();
}
}
现在您可以像这样调用“排序”:
Sort(SortOrder.Descending, x => x.Album);
更新2
跟进Tom Lokhorst的评论:如果你想预先定义一些速记排序标准,你可以通过定义这样一个类来实现:
public static class SortColumn
{
public static readonly Func<Song, string> Artist = x => x.Artist;
public static readonly Func<Song, string> Album = x => x.Album;
}
现在您只需致电:
Sort(SortOrder.Descending, SortColumn.Artist);
答案 1 :(得分:2)
您可以尝试使用generic comparer。
答案 2 :(得分:0)
我认为您应该向IList<T>
添加扩展方法:
public static class extIList {
public static IList<T> Sort<T, TKey>(this IList<T> list, SortOrder sortOrder, Func<T, TKey> keySelector) {
if (sortOrder == SortOrder.Descending) {
return list.OrderByDescending(keySelector).ToList();
} else {
return list.OrderBy(keySelector).ToList();
}
}
}
然后你可以使用漂亮的每个对象:
IList<Person> list = new List<Person>();
list.Add(new Person("David","Beckham"));
list.Add(new Person("Gennaro","Gattuso"));
list.Add(new Person("Cristian","Carlesso"));
list = list.Sort(SortOrder.Descending, X => X.Name);
ps SortOrder已经存在:
using System.Data.SqlClient;
答案 3 :(得分:-1)
如果您有多种专用方法,那么排序就像生活一样。也许他们可以被聚集到一个班级。
public enum SortOrder
{
Ascending = 0,
Descending = 1
}
public class Sorter<T>
{
public SortOrder Direction { get; set; }
public Func<T, object> Target { get; set; }
public Sorter<T> NextSort { get; set; }
public IOrderedEnumerable<T> ApplySorting(IEnumerable<T> source)
{
IOrderedEnumerable<T> result = Direction == SortOrder.Descending ?
source.OrderByDescending(Target) :
source.OrderBy(Target);
if (NextSort != null)
{
result = NextSort.ApplyNextSorting(result);
}
return result;
}
private IOrderedEnumerable<T> ApplyNextSorting
(IOrderedEnumerable<T> source)
{
IOrderedEnumerable<T> result = Direction == SortOrder.Descending ?
source.ThenByDescending(Target) :
source.ThenBy(Target);
return result;
}
}
以下是示例用法:
List<string> source = new List<string>()
{ "John", "Paul", "George", "Ringo" };
Sorter<string> mySorter = new Sorter<string>()
{
Target = s => s.Length,
NextSort = new Sorter<string>()
{
Direction = SortOrder.Descending,
Target = s => s
}
};
foreach (string s in mySorter.ApplySorting(source))
{
Console.WriteLine(s);
}
输出是Paul,John,Ringo,George。