c# List Sort排序
Test test1 = new Test(); test1.sortIndex = 1; test1.name = "1"; Test test2 = new Test(); test2.sortIndex = 2; test2.name = "2"; Test test3 = new Test(); test3.sortIndex = 3; test3.name = "3"; Test test4 = new Test(); test4.sortIndex = 4; test4.name = "4"; List<Test> list = new List<Test>(); list.Add(test4); list.Add(test1); list.Add(test2); list.Add(test3); list.Sort((a, b) => { var o = a.sortIndex - b.sortIndex; return o; });
这里是用
Comparison委托
Comparison委托的定义如下:public delegate int Comparison<in T>(T x, T y);
使用委托,可以传递一个与委托签名相同的函数,可以使用匿名委托,还可以用Lambda表达式