一、背景
有时用户需要按某项排序,但是查询结果以List格式存储,我们当然可以自己编写一个快速排序的方法进行排序,但是还有多个选择,并且可能比你写的短、效率也不差,那不如在恰当的时候选择其他方法对List进行排序
二、方法
第一种: List的OrderBy与OrderByDescending方法
个人认为如果只是简单的进行排序,并且并不想影响实体类的话,这个方法是最有效的,只需一句话便可解决排序问题。其中OrderBy
是升序操作、OrderByDescending
是降序操作。然后在其中使用lambda表达式即可完成。示例代码如下所示:
public class ListTest
{
public static void SortList()
{
List<CapVal> list = new List<CapVal>();
list.Add(new CapVal("一月", 3));
list.Add(new CapVal("二月", 21));
list.Add(new CapVal("三月", 12));
list.Add(new CapVal("四月", 6));
list.Add(new CapVal("五月", 9));
list.Add(new CapVal("六月", 3));
list.Add(new CapVal("七月", 17));
list.Add(new CapVal("八月", 29));
list.Add(new CapVal("九月", 19));
Console.WriteLine("排序前——————" );
foreach(CapVal item in list)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("升序排序序后——————");
List<CapVal> upList = list.OrderBy(a => a.Value).ToList();
foreach (CapVal item in upList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("降序排序序后——————");
List<CapVal> downList = list.OrderByDescending(a => a.Value).ToList();
foreach (CapVal item in downList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.ReadKey();
}
}
public class CapVal
{
public String Caption { get; set; }
public decimal Value { get; set; }
public CapVal(String _caption, decimal _value)
{
this.Caption = _caption;
this.Value = _value;
}
}
运行结果如图所示:
第二种: 使用Sort()方法
首先关于Sort也有几种用法,接下来举出使用最多的几种方法
方法一:使用Lambda表达式
利用Lambda和CompareTo进行编写即可得到排序结果,值得注意的是降序和排序主要依靠两个实体的位置,例如:
list.Sort((a, b) => a.Value.CompareTo(b.Value));//升序
list.Sort((a, b) => b.Value.CompareTo(a.Value));//降序
具体的例子如下所示:
public static void SortList()
{
List<CapVal> list = new List<CapVal>();
list.Add(new CapVal("一月", 3));
list.Add(new CapVal("二月", 21));
list.Add(new CapVal("三月", 12));
list.Add(new CapVal("四月", 6));
list.Add(new CapVal("五月", 9));
list.Add(new CapVal("六月", 3));
list.Add(new CapVal("七月", 17));
list.Add(new CapVal("八月", 29));
list.Add(new CapVal("九月", 19));
Console.WriteLine("排序前——————" );
foreach(CapVal item in list)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("升序排序序后——————");
list.Sort((a, b) => a.Value.CompareTo(b.Value));
List<CapVal> upList = list;
foreach (CapVal item in upList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("降序排序序后——————");
list.Sort((a, b) => b.Value.CompareTo(a.Value));
List<CapVal> downList = list;
foreach (CapVal item in downList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.ReadKey();
}
执行结果如图所示:
方法二:使用 IComparer
使用这个方法需要继承IComparer创建方法,使用的时候创建新的方法就可以了。首先需要创建继承的IComparer方法,具体代码如下:
public class UpCapValComparer : IComparer<CapVal>
{
public int Compare(CapVal cv1, CapVal cv2)
{
return cv1.Value.CompareTo(cv2.Value);
}
}
public class DownCapValComparer : IComparer<CapVal>
{
public int Compare(CapVal cv1, CapVal cv2)
{
return cv2.Value.CompareTo(cv1.Value);
}
}
调用如下:
public static void SortList()
{
List<CapVal> list = new List<CapVal>();
list.Add(new CapVal("一月", 3));
list.Add(new CapVal("二月", 21));
list.Add(new CapVal("三月", 12));
list.Add(new CapVal("四月", 6));
list.Add(new CapVal("五月", 9));
list.Add(new CapVal("六月", 3));
list.Add(new CapVal("七月", 17));
list.Add(new CapVal("八月", 29));
list.Add(new CapVal("九月", 19));
Console.WriteLine("排序前——————" );
foreach(CapVal item in list)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("升序排序序后——————");
list.Sort(new UpCapValComparer());
List<CapVal> upList = list;
foreach (CapVal item in upList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("降序排序序后——————");
list.Sort(new DownCapValComparer());
List<CapVal> downList = list;
foreach (CapVal item in downList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.ReadKey();
}
结果如图:
方法三:使用委托
思路与方法二类似,只不过省略了继承编写IComparer方法类,改为一句话在方法内编写。如下的代码就是本次所使用的例子:
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });
具体代码如下所示:
public static void SortList()
{
List<CapVal> list = new List<CapVal>();
list.Add(new CapVal("一月", 3));
list.Add(new CapVal("二月", 21));
list.Add(new CapVal("三月", 12));
list.Add(new CapVal("四月", 6));
list.Add(new CapVal("五月", 9));
list.Add(new CapVal("六月", 3));
list.Add(new CapVal("七月", 17));
list.Add(new CapVal("八月", 29));
list.Add(new CapVal("九月", 19));
Console.WriteLine("排序前——————" );
foreach(CapVal item in list)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("升序排序序后——————");
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });
List<CapVal> upList = list;
foreach (CapVal item in upList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("降序排序序后——————");
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv2.Value.CompareTo(cv1.Value); });
List<CapVal> downList = list;
foreach (CapVal item in downList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.ReadKey();
}
结果如下:
第三种: 使用Linq排序
c#的一大特色就是Linq,他遍布c#里里外外,利用他可以减少大量的代码,不过他是从SQL中出来的,所以SQL还是了解之后,这个就好懂了。具体代码如下所示:
public static void SortList()
{
List<CapVal> list = new List<CapVal>();
list.Add(new CapVal("一月", 3));
list.Add(new CapVal("二月", 21));
list.Add(new CapVal("三月", 12));
list.Add(new CapVal("四月", 6));
list.Add(new CapVal("五月", 9));
list.Add(new CapVal("六月", 3));
list.Add(new CapVal("七月", 17));
list.Add(new CapVal("八月", 29));
list.Add(new CapVal("九月", 19));
Console.WriteLine("排序前——————" );
foreach(CapVal item in list)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("升序排序序后——————");
List<CapVal> upList = (from cv in list
orderby cv.Value ascending
select cv).ToList();
foreach (CapVal item in upList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.WriteLine("降序排序序后——————");
List<CapVal> downList = (from cv in list
orderby cv.Value descending
select cv).ToList();
foreach (CapVal item in downList)
{
Console.WriteLine(item.Caption + ":" + item.Value);
}
Console.ReadKey();
}
运行结果如图:
三、总结
排序方法还有其他的,这里没有完整举出,多几个选择总比只有一个好。