这里有很多种方法对List进行排序,本文总结了三种方法,但多种实现。
方法一:
调用sort方法,如果需要降序,进行反转:
List list = new List();
list.Sort();// 升序排序
list.Reverse();// 反转顺序
方法二:
使用lambda表达式,在前面加个负号就是降序了
List list= new List(){5,1,22,11,4};
list.Sort((x, y) => x.CompareTo(y));//升序
list.Sort((x, y) => -x.CompareTo(y));//降序
接下来是对非基本类型排序,以一个类为例。
首先写一个类用于排序,里面有两个属性,一个构造方法,重写了ToString方法:
class People
{
private int _id;
private string _name;
public People(int id,string name)
{
this._id = id;
this.Name = name;
}
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
//重写ToString
public override string ToString()
{
return "ID:"+_id+" Name:"+_name;
}
}
然后添加一些随机数据,仍希望用Sort排序
List list = new List();
Random r = new Random();
//添加数据
for(int i = 0; i < 10; i++)
{
int j = r.Next(0, 10);
list.Add(new People(j, "name" + j));
}
Console.WriteLine("排序前:");
foreach(var p in list)
{
Console.WriteLine(p);
}
list.Sort();//排序
Console.WriteLine("排序后:");
foreach (var p in list)
{
Console.WriteLine(p);
}
很不幸,前面输出正常,后面抛异常了:
查看Sort源码可知它有如下几个重载:
第三和第四个差不多。
可以看到它只有一个方法,我们只需要修改类本身
class People: IComparable
{
private int _id;
private string _name;
public People(int id,string name)
{
this._id = id;
this.Name = name;
}
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
//重写的CompareTo方法,根据Id排序
public int CompareTo(People other)
{
if (null == other)
{
return 1;//空值比较大,返回1
}
//return this.Id.CompareTo(other.Id);//升序
return other.Id.CompareTo(this.Id);//降序
}
//重写ToString
public override string ToString()
{
return "ID:"+_id+" Name:"+_name;
}
}
我们首先来看看这个接口:
public interface IComparer<in T>
{
// Parameters:
// x:
// The first object to compare.
//
// y:
// The second object to compare.
//
// Returns:
// A signed integer that indicates the relative values of x and y, as shown in the
// following table.Value Meaning Less than zerox is less than y.Zerox equals y.Greater
// than zerox is greater than y.
int Compare(T x, T y);
}
重点就看返回值,小于0代表x < y,等于0代表x=y,大于0代表x > y.
下面看一下类的实现,非常简单,一句代码:
class People:IComparer
{
private int _id;
private string _name;
public People()
{
}
public People(int id,string name)
{
this._id = id;
this.Name = name;
}
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
//Compare函数
public int Compare(People x, People y)
{
return x.Id.CompareTo(y.Id);//升序
}
//重写ToString
public override string ToString()
{
return "ID:"+_id+" Name:"+_name;
}
}
但是还没完,我们其实是用了第2点说的第一个重载方法,所以List还需要参数:
IComparer comparer = new People();
list.Sort(comparer);
虽然想实现排序上面的接口代码也不多,但有时候只是偶尔排序,并不像修改类,怎么办呢?当然有更简单的方法,委托和lambda表达式:
所以就有了下面的代码,不需要修改类,只需要用委托构造重载而已:
list.Sort(
delegate(People p1,People p2)
{
return p1.Id.CompareTo(p2.Id);//升序
}
);
当然,lambda表达式实现更简单:
list.Sort((x,y)=> { return x.Id.CompareTo(y.Id); });
此方法将排序好的list再赋给原来的list,也可以给其他的。
list = list.OrderBy(o => o.Id).ToList();//升序
list = list.OrderByDescending(o => o.Id).ToList();//降序
排序的方法我就知道这么多了(其实有更多),接下来还有一个问题,如果希望当ID相同时比较Name,上面的代码就需要改改了。
其中,接口IComparable这样写:
//重写的CompareTo方法,根据Id排序
public int CompareTo(People other)
{
if (null == other)
{
return 1;//空值比较大,返回1
}
//等于返回0
int re = this.Id.CompareTo(other.Id);
if (0 == re)
{
//id相同再比较Name
return this.Name.CompareTo(other.Name);
}
return re;
}
IComparer和delegate还有lambda里可以这样:
public int Compare(People x, People y)
{
int re = x.Id.CompareTo(y.Id);
if (0 == re)
{
return x.Name.CompareTo(y.Name);
}
return re;
}
OrderBy方法有点不同:
list = list.OrderBy(o => o.Id).ThenBy(o=>o.Name).ToList();
list = list.OrderByDescending(o => o.Id).ThenByDescending(o=>o.Name).ToList();//降序
虽然说了那么多,其实说到底也就三种方法,两个接口和OrderBy方法,lambda表达式只是让形式更简单。