list排序的三种实现方式
2015-01-20 09:03
453 查看
第一种:实体类实现IComparable接口,而且必须实现CompareTo方法
实体类定义如下:class Info:IComparable
{
public int Id { get; set; }
public string Name { get; set; }
public int CompareTo(object obj) {
int result;
try
{
Info info = obj as Info;
if (this.Id > info.Id)
{
result = 0;
}
else
result = 1;
return result;
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
}调用方式如下,只需要用sort方法就能实现对list进行排序。
private static void ReadAccordingCompare() {
List<Info> infoList = new List<Info>();
infoList.Add(
new Info() { Id = 1, Name = "abc" });
infoList.Add(new Info() { Id = 3, Name = "rose" });
infoList.Add(new Info() { Id = 2, Name = "woft" });
infoList.Sort();
foreach (var item in infoList)
{
Console.WriteLine(item.Id + ":" + item.Name);
}
}
第二种方法:linq to list进行排序
运用linq实现对list排序,在实体类定义的时候就不需用实现IComparable接口,调用方式如下:
但是上面两种方式都只能对一个实体属性排序,如果对不同的属性排序的话只能写很多的if进行判断,这样显得很麻烦。
且看下面的方式实现根据传入参数进行排序。
ListSort("Name","desc");//表示对Name进行desc排序
ListSort("Id","asc");//表示对Id进行asc排序。如此如果参数很多的话减少了很多判断。
后续更新
private static void ListSort(string field,string rule)
{
if (!string.IsNullOrEmpty(rule) && (rule.ToLower().Equals("desc") || rule.ToLower().Equals("asc")))
{
try
{
List<Info> infoList = GetList();
infoList.Sort(
delegate(Info info1, Info info2)
{
Type t = typeof(Info);
PropertyInfo pro = t.GetProperty(field);
return rule.ToLower().Equals("asc") ?
pro.GetValue(info1, null).ToString().CompareTo(pro.GetValue(info2, null).ToString()) :
pro.GetValue(info2, null).ToString().CompareTo(pro.GetValue(info1, null).ToString());
});
Console.WriteLine("*****ListSort**********");
foreach (var item in infoList)
{
Console.WriteLine(item.Id + "," + item.Name);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ba3b
}
}
else
Console.WriteLine("ruls is wrong");
}
实体类定义如下:class Info:IComparable
{
public int Id { get; set; }
public string Name { get; set; }
public int CompareTo(object obj) {
int result;
try
{
Info info = obj as Info;
if (this.Id > info.Id)
{
result = 0;
}
else
result = 1;
return result;
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
}调用方式如下,只需要用sort方法就能实现对list进行排序。
private static void ReadAccordingCompare() {
List<Info> infoList = new List<Info>();
infoList.Add(
new Info() { Id = 1, Name = "abc" });
infoList.Add(new Info() { Id = 3, Name = "rose" });
infoList.Add(new Info() { Id = 2, Name = "woft" });
infoList.Sort();
foreach (var item in infoList)
{
Console.WriteLine(item.Id + ":" + item.Name);
}
}
第二种方法:linq to list进行排序
运用linq实现对list排序,在实体类定义的时候就不需用实现IComparable接口,调用方式如下:
private static void ReadT(string str) { List<Info> infoList = new List<Info>(); infoList.Add( new Info() { Id = 1, Name = "woft" }); infoList.Add(new Info() { Id=3,Name="rose"}); infoList.Add(new Info() { Id = 2, Name = "abc" }); Console.WriteLine("ReadT*********************"); IEnumerable<Info> query = null; query = from items in infoList orderby items.Id select items; foreach (var item in query) { Console.WriteLine(item.Id+":"+item.Name); } }
但是上面两种方式都只能对一个实体属性排序,如果对不同的属性排序的话只能写很多的if进行判断,这样显得很麻烦。
且看下面的方式实现根据传入参数进行排序。
private static void ListSort(string field,string rule) { if (!string.IsNullOrEmpty(rule)&&(!rule.ToLower().Equals("desc")||!rule.ToLower().Equals("asc"))) { try { List<Info> infoList = GetList(); infoList.Sort( delegate(Info info1, Info info2) { Type t1 = info1.GetType(); Type t2 = info2.GetType(); PropertyInfo pro1 = t1.GetProperty(field); PropertyInfo pro2 = t2.GetProperty(field); return rule.ToLower().Equals("asc") ? pro1.GetValue(info1, null).ToString().CompareTo(pro2.GetValue(info2, null).ToString()) : pro2.GetValue(info2, null).ToString().CompareTo(pro1.GetValue(info1, null).ToString()); }); Console.WriteLine("*****ListSort**********"); foreach (var item in infoList) { Console.WriteLine(item.Id + "," + item.Name); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.WriteLine("ruls is wrong"); }调用方式:
ListSort("Name","desc");//表示对Name进行desc排序
ListSort("Id","asc");//表示对Id进行asc排序。如此如果参数很多的话减少了很多判断。
后续更新
private static void ListSort(string field,string rule)
{
if (!string.IsNullOrEmpty(rule) && (rule.ToLower().Equals("desc") || rule.ToLower().Equals("asc")))
{
try
{
List<Info> infoList = GetList();
infoList.Sort(
delegate(Info info1, Info info2)
{
Type t = typeof(Info);
PropertyInfo pro = t.GetProperty(field);
return rule.ToLower().Equals("asc") ?
pro.GetValue(info1, null).ToString().CompareTo(pro.GetValue(info2, null).ToString()) :
pro.GetValue(info2, null).ToString().CompareTo(pro.GetValue(info1, null).ToString());
});
Console.WriteLine("*****ListSort**********");
foreach (var item in infoList)
{
Console.WriteLine(item.Id + "," + item.Name);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ba3b
}
}
else
Console.WriteLine("ruls is wrong");
}
相关文章推荐
- c# list排序的三种实现方式
- c# list排序的三种实现方式
- c# list排序的三种实现方式
- list排序的三种实现方式
- c# list排序的三种实现方式 (转帖)
- c# list排序的三种实现方式
- c# list排序的三种实现方式
- c# list排序的三种实现方式
- c# list排序的三种实现方式
- java基础 三种方式对int类型的List集合排序(通用排序)
- Repeater排序的三种实现方式(附在线Demo)
- java 实现List对象中按某种方式进行排序
- 三种对list排序的方法,并实现动态传递字段名对list进行排序
- java的list几种实现方式的效率(ArrayList、LinkedList、Vector、Stack),以及 java时间戳的三种获取方式比较
- 数组三种方式排序实现
- 快速排序的三种实现方式以及非递归版本
- c# list排序的实现方式
- 最简单的对Java List列表按中文拼音排序的实现方式
- 120941138:java list sort()排序各种方式,实现接口
- Repeater排序的三种实现方式