有时用户需要按某项排序,但是查询结果以List格式存储,我们当然可以自己编写一个快速排序的方法进行排序,但是还有多个选择,并且可能比你写的短、效率也不差,那不如在恰当的时候选择其他方法对List进行排序。
1,对于普通的int,double型列表List,如下:
List<int> list = new List<int>();
list.Sort(); // 升序排序
list.Reverse(); // 降序排序:先升序排序,再反转顺序
1
2
3
2,对于Class型的列表List,根据类的某一属性进行排序,如下:
方法一: List的OrderBy与OrderByDescending方法
list.OrderBy(a => a.Value).ToList();//升序
list.OrderByDescending(a => a.Value).ToList();//降序
1
2
方法二:使用Sort方法,
list.Sort((a, b) => a.Value.CompareTo(b.Value));//升序
list.Sort((a, b) => b.Value.CompareTo(a.Value));//降序
1
2
方法三:使用委托
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });
1
方法四:使用Linq
c#的一大特色就是Linq,他遍布c#里里外外,利用他可以减少大量的代码,不过他是从SQL中出来的,所以SQL还是了解之后,这个就好懂了。具体代码如下所示:
List<CapVal> upList = (from cv in list
orderby cv.Value ascending
select cv).ToList();//升序
1
2
3
List<CapVal> downList = (from cv in list
orderby cv.Value descending
select cv).ToList();//降序
1
2
3
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("升序排序序后———OrderBy———");
List<CapVal> upList = list.OrderBy(a => a.Value).ToList();
Console.WriteLine("升序排序序后———Sort———");
list.Sort((a, b) => a.Value.CompareTo(b.Value));
List<CapVal> upList = list;
Console.WriteLine("升序排序序后———使用委托———");
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });
List<CapVal> upList = list;
Console.WriteLine("升序排序序后———使用Linq———");
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("降序排序序后———OrderBy———");
List<CapVal> downList = list.OrderByDescending(a => a.Value).ToList();
Console.WriteLine("降序排序序后———Sort———");
list.Sort((a, b) => b.Value.CompareTo(a.Value));
List<CapVal> downList = list;
Console.WriteLine("降序排序序后———使用委托———");
list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv2.Value.CompareTo(cv1.Value); });
List<CapVal> downList = list;
Console.WriteLine("降序排序序后———使用Linq———");
List<CapVal> downList = (from cv in list
orderby cv.Value descending
select cv).ToList();//降序
List<CapVal> downList = list;
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;
}
}
————————————————
版权声明:本文为CSDN博主「Pass_Time_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45499836/article/details/126481313
查看更多关于C# List排序简介及四种方法介绍-附代码的详细内容...