c# list排序的三种实现方式
2015-08-13 16:19
661 查看
原文地址:http://www.cnblogs.com/bradwarden/archive/2012/06/19/2554854.html
第一种:实体类实现IComparable接口,而且必须实现CompareTo方法
1 class Info:IComparable
2 {
3 public
int Id { get; set; }
4 public
string Name { get; set; }
5
6 public
int CompareTo(object obj) {
7 int result;
8 try
9 {
10 Info info = obj
as Info;
11 if (this.Id > info.Id)
12 {
13 result =
0;
14 }
15 else
16 result =
1;
17 return result;
18 }
19 catch (Exception ex) {
throw new Exception(ex.Message); }
20 }
21 }
调用方式如下,只需要用sort方法就能实现对list进行排序。
1 private
static void ReadAccordingCompare() {
2 List<Info> infoList =
new List<Info>();
3 infoList.Add(
4 new Info() { Id =
1, Name = "abc" });
5 infoList.Add(new Info() { Id =
3, Name = "rose" });
6 infoList.Add(new Info() { Id =
2, Name = "woft" });
7 infoList.Sort();
8 foreach (var item
in infoList)
9 {
10 Console.WriteLine(item.Id +
":" + item.Name);
11 }
12 }
第二种方法:linq to list进行排序
运用linq实现对list排序,在实体类定义的时候就不需用实现IComparable接口,调用方式如下:
1 private
static void ReadT(string str) {
2 List<Info> infoList =
new List<Info>();
3 infoList.Add(
4 new Info() { Id =
1, Name = "woft" });
5 infoList.Add(new Info() { Id=3,Name="rose"});
6 infoList.Add(new Info() { Id =
2, Name = "abc" });
7 Console.WriteLine("ReadT*********************");
8 IEnumerable<Info> query =
null;
9
query = from items in infoList
orderby items.Id select items;
10
foreach (var item in query)
11 {
12 Console.WriteLine(item.Id+":"+item.Name);
13 }
14 }
上面两种方式都只能对一个实体属性排序,如果对不同的属性排序的话只能写很多的if进行判断,这样显得很麻烦。
且看下面的方式实现根据传入参数进行排序。
private static void ListSort(string field,string rule)
2 {
3
if (!string.IsNullOrEmpty(rule) && (rule.ToLower().Equals("desc") || rule.ToLower().Equals("asc")))
4 {
5 try
6 {
7 List<Info> infoList = GetList();
8
infoList.Sort(
9
delegate(Info info1, Info info2)
10 {
11 Type t =
typeof(Info);
12 PropertyInfo pro = t.GetProperty(field);
13
return rule.ToLower().Equals("asc") ?
14 pro.GetValue(info1,
null).ToString().CompareTo(pro.GetValue(info2,
null).ToString()) :
15 pro.GetValue(info2,
null).ToString().CompareTo(pro.GetValue(info1,
null).ToString());
16 });
17 Console.WriteLine("*****ListSort**********");
18 foreach (var item
in infoList)
19 {
20 Console.WriteLine(item.Id +
"," + item.Name);
21 }
22 }
23 catch (Exception ex)
24 {
25 Console.WriteLine(ex.Message);
26 }
27 }
28 else
29 Console.WriteLine("ruls is wrong");
30 }
调用方式:
ListSort("Name","desc");//表示对Name进行desc排序
ListSort("Id","asc");//表示对Id进行asc排序。如此如果参数很多的话减少了很多判断。
第一种:实体类实现IComparable接口,而且必须实现CompareTo方法
1 class Info:IComparable
2 {
3 public
int Id { get; set; }
4 public
string Name { get; set; }
5
6 public
int CompareTo(object obj) {
7 int result;
8 try
9 {
10 Info info = obj
as Info;
11 if (this.Id > info.Id)
12 {
13 result =
0;
14 }
15 else
16 result =
1;
17 return result;
18 }
19 catch (Exception ex) {
throw new Exception(ex.Message); }
20 }
21 }
调用方式如下,只需要用sort方法就能实现对list进行排序。
1 private
static void ReadAccordingCompare() {
2 List<Info> infoList =
new List<Info>();
3 infoList.Add(
4 new Info() { Id =
1, Name = "abc" });
5 infoList.Add(new Info() { Id =
3, Name = "rose" });
6 infoList.Add(new Info() { Id =
2, Name = "woft" });
7 infoList.Sort();
8 foreach (var item
in infoList)
9 {
10 Console.WriteLine(item.Id +
":" + item.Name);
11 }
12 }
第二种方法:linq to list进行排序
运用linq实现对list排序,在实体类定义的时候就不需用实现IComparable接口,调用方式如下:
1 private
static void ReadT(string str) {
2 List<Info> infoList =
new List<Info>();
3 infoList.Add(
4 new Info() { Id =
1, Name = "woft" });
5 infoList.Add(new Info() { Id=3,Name="rose"});
6 infoList.Add(new Info() { Id =
2, Name = "abc" });
7 Console.WriteLine("ReadT*********************");
8 IEnumerable<Info> query =
null;
9
query = from items in infoList
orderby items.Id select items;
10
foreach (var item in query)
11 {
12 Console.WriteLine(item.Id+":"+item.Name);
13 }
14 }
上面两种方式都只能对一个实体属性排序,如果对不同的属性排序的话只能写很多的if进行判断,这样显得很麻烦。
且看下面的方式实现根据传入参数进行排序。
private static void ListSort(string field,string rule)
2 {
3
if (!string.IsNullOrEmpty(rule) && (rule.ToLower().Equals("desc") || rule.ToLower().Equals("asc")))
4 {
5 try
6 {
7 List<Info> infoList = GetList();
8
infoList.Sort(
9
delegate(Info info1, Info info2)
10 {
11 Type t =
typeof(Info);
12 PropertyInfo pro = t.GetProperty(field);
13
return rule.ToLower().Equals("asc") ?
14 pro.GetValue(info1,
null).ToString().CompareTo(pro.GetValue(info2,
null).ToString()) :
15 pro.GetValue(info2,
null).ToString().CompareTo(pro.GetValue(info1,
null).ToString());
16 });
17 Console.WriteLine("*****ListSort**********");
18 foreach (var item
in infoList)
19 {
20 Console.WriteLine(item.Id +
"," + item.Name);
21 }
22 }
23 catch (Exception ex)
24 {
25 Console.WriteLine(ex.Message);
26 }
27 }
28 else
29 Console.WriteLine("ruls is wrong");
30 }
调用方式:
ListSort("Name","desc");//表示对Name进行desc排序
ListSort("Id","asc");//表示对Id进行asc排序。如此如果参数很多的话减少了很多判断。
相关文章推荐
- C# 字符串的长度问题
- .net快速创建PDF文档 by c#
- Luhn算法 C#实现 信用卡校验
- [转]正则表达式相关:C# 抓取网页类(获取网页中所有信息)
- C# GET 和 SET作用
- C# 代码片段
- Visual C#2010学习笔记六之运算符的重载
- C#实现求一组数据众数的方法
- C#自动添加using引用命名空间
- C# winform 实现图片轮播
- C#错误---线程间操作无效: 从不是创建控件"Form1"的线程访问它.
- c#软件实现语言版本切换
- C#读取csv文件内容到DataTable
- C#实现将浮点数表示的货币数量以汉字大写形式输出的方法
- C#双向链表LinkedList排序实现方法
- XmlDocument在C#中的应用
- c#日期和随机数--》的士发票
- C#实现计算一个点围绕另一个点旋转指定弧度后坐标值的方法
- c# winform richtextbox 滚动条 最后位置
- c# winform 模拟web post图片