不太明白你想问的问题什么意思,给你看下别人总结的sort3种重载的举例,希望对你有帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ListSort
{
class Program
{
static void Main(string[] args)
{
ListL = new List ();
L.Add(new C { n = 1, s = "b" });
L.Add(new C { n = 3, s = "a" });
L.Add(new C { n = 2, s = "c" });
// 方法1 使用Comparison委托。下面代码使用的是lamda表达是。也可以使用匿名委托之类的,效果是一样的。
L.Sort((left, right) =>
{
if (left.n > right.n)
return 1;
else if (left.n == right.n)
return 0;
else
return -1;
});
// 方法2 使用IComparer接口。
L.Sort(new CComparer());
// 方法3 除以上两种方法以外还可以使用另一种方法,在C类中实现IComparable
L.Sort();
L.ForEach((c) =>
{
Console.WriteLine(c.n);
});
Console.Read();
}
}
public class C : IComparable
{
public int n;
public string s;
public int CompareTo(C other)
{
if (this.n > other.n)
return 1;
else if (this.n == other.n)
return 0;
else
return -1;
}
}
public class CComparer : IComparer
{
public int Compare(C left, C right)
{
if (left.n > right.n)
return 1;
else if (left.n == right.n)
return 0;
else
return -1;
}
}
}