C#中的基本类型都提供了默认的比较算法,C#可以调用比较算法为基本类型的数组进行排序。
若希望对自建类进行比较或排序,那么可以使用IComparable<T>和IComparer<T>接口。
一、IComparable<T>接口
继承IComparable<T>接口,可以给自建类实现一个通用的比较方法,使自建类的数组或List<T>/SortList<K,V>的元素,使用Sort方法或者SortList<K,V>的内部排序机制来自定义此数据类型在数组中的排序方式。
实现IComparable<T>接口,要求在类中实现CompareTo方法,该方法参数是一个T类型的对象,返回值必须是-1,0,1中之一。
在要比较的对象的类中实现,可以比较该对象和另一个对象,具体实现示例如下:
//定义由值类型或类实现的通用的比较方法,以为排序实例创建类型特定的比较方法.
public class Square : IComparable<Square>
{
public Square() { }
public Square(int height, int width)
{
this.Height = height;
this.Width = width;
}
public int Height { get; set; }
public int Width { get; set; }
public int CompareTo(object obj)
{
Square square = obj as Square;
if (square != null)
{
return CompareTo(square);
}
throw
new ArgumentException("Both objects being compared must be of type Square.");
}
public int CompareTo(Square other)
{
long area1 = this.Height * this.Width;
long area2 = other.Height * other.Width;
if (area1 == area2)
return 0;
else if (area1 > area2)
return 1;
else if (area1 < area2)
return -1;
else
return -1;
}
//public override string ToString() => ("Height: {this.Height} Width: {this.Width}");
public override string ToString()
{
return String.Format("The Square is (Height: {0}, Width: {1}).", this.Height, this.Width);
}
}
利用Sort()方法进行排序:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestSort();
}
public static void TestSort()
{
List<Square> listOfSquares = new List<Square>(){
new Square(1,3),
new Square(4,3),
new Square(2,1),
new Square(6,1)};
//测试List<String>
Console.WriteLine("List<String>");
Console.WriteLine("Original list");
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine();
Console.WriteLine("Sorted list using IComparable<Square>");
listOfSquares.Sort();
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
}
}
}
运行结果:
List<String>
Original list
The Square is (Height: 1, Width: 3).
The Square is (Height: 4, Width: 3).
The Square is (Height: 2, Width: 1).
The Square is (Height: 6, Width: 1).
Sorted list using IComparable<Square>
The Square is (Height: 2, Width: 1).
The Square is (Height: 1, Width: 3).
The Square is (Height: 6, Width: 1).
The Square is (Height: 4, Width: 3).
请按任意键继续. . .
二、IComparer<T>接口
IComparer<T>可以提供了Compare方法,并且可以接受参数,以用户需要的比较类型排序。
在一个单独的类中实现,可以比较任意两个对象。
//定义类型为比较两个对象而实现的方法.
public class CompareHeight : IComparer<Square>
{
public int Compare(object firstSquare, object secondSquare)
{
Square Square1 = firstSquare as Square;
Square Square2 = secondSquare as Square;
if (Square1 == null || Square2 == null)
throw (new ArgumentException("Both parameters must be of type Square."));
else
return Compare(firstSquare, secondSquare);
}
#region IComparer<Square> Members
public int Compare(Square x, Square y)
{
if (x.Height == y.Height)
return 0;
else if (x.Height > y.Height)
return 1;
else if (x.Height < y.Height)
return -1;
else
return -1;
}
#endregion
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestSort();
}
public static void TestSort()
{
List<Square> listOfSquares = new List<Square>(){
new Square(1,3),
new Square(4,3),
new Square(2,1),
new Square(6,1)};
//测试List<String>
Console.WriteLine("List<String>");
Console.WriteLine("Original list");
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine();
IComparer<Square> heightCompare = new CompareHeight();
listOfSquares.Sort(heightCompare);
Console.WriteLine("Sorted list using IComparer<Square>=heightCompare");
foreach (Square square in listOfSquares)
{
Console.WriteLine(square.ToString());
}
Console.WriteLine();
}
}
}
运行结果:
List<String>
Original list
The Square is (Height: 1, Width: 3).
The Square is (Height: 4, Width: 3).
The Square is (Height: 2, Width: 1).
The Square is (Height: 6, Width: 1).
Sorted list using IComparer<Square>=heightCompare
The Square is (Height: 1, Width: 3).
The Square is (Height: 2, Width: 1).
The Square is (Height: 4, Width: 3).
The Square is (Height: 6, Width: 1).
请按任意键继续. . .
最后,测试SortList<K,V>,如下:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TestSort();
}
public static void TestSort()
{
//测试SortedList
var sortedListOfSquares = new SortedList<int,Square>(){
{ 0, new Square(1,3)},
{ 2, new Square(3,3)},
{ 1, new Square(2,1)},
{ 3, new Square(6,1)}};
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("SortedList<Square>");
foreach (KeyValuePair<int,Square> kvp in sortedListOfSquares)
{
Console.WriteLine ("kvp.Key: {0} kvp.Value: {1}",kvp.Key,kvp.Value);
}
}
}
}
运行结果:
SortedList<Square>
kvp.Key: 0 kvp.Value: The Square is (Height: 1, Width: 3).
kvp.Key: 1 kvp.Value: The Square is (Height: 2, Width: 1).
kvp.Key: 2 kvp.Value: The Square is (Height: 3, Width: 3).
kvp.Key: 3 kvp.Value: The Square is (Height: 6, Width: 1).
请按任意键继续. . .