C#对List进行排序

C#对List进行排序

实现方法直接调用List中的Sort方法,然后比较值得大小。

 

0001升序:

AllTLRectangle.Sort(delegate (TLRectangle x, TLRectangle y)

{

return x.Cente.CompareTo(y.Cente);

});

0002降序:

AllTLRectangle.Sort(delegate (TLRectangle x, TLRectangle y)

{

return y.Cente.CompareTo(x.Cente);

});

0002 Sort的详解

//

// 摘要:

// 使用指定的比较器对 System.Collections.Generic.List`1 中某个范围内的元素进行排序。

//

// 参数:

// index:

// 要排序范围的从零开始的起始索引。

//

// count:

// 要排序的范围的长度。

//

// comparer:

// 比较元素时要使用的 System.Collections.Generic.IComparer`1 实现,若要使用默认比较器 System.Collections.Generic.Comparer`1.Default,则为

// null。

//

// 异常:

// T:System.ArgumentOutOfRangeException:

// index 小于 0。- 或 -count 小于 0。

//

// T:System.ArgumentException:

// index 和 count 未指定中的有效范围 System.Collections.Generic.List`1。- 或 -实现 comparer 在排序期间导致了错误。例如,

// comparer 比较某个项与其自身时可能不会返回 0。

//

// T:System.InvalidOperationException:

// comparer 是 null, ,和默认比较器 System.Collections.Generic.Comparer`1.Default 找不到实现

// System.IComparable`1 泛型接口或 System.IComparable 类型的接口 T。

public void Sort(int index, int count, IComparer<T> comparer);

//

// 摘要:

// 使用指定的 System.Comparison`1,对整个 System.Collections.Generic.List`1 中的元素进行排序。

//

// 参数:

// comparison:

// 比较元素时要使用的 System.Comparison`1。

//

// 异常:

// T:System.ArgumentNullException:

// comparison 为 null。

//

// T:System.ArgumentException:

// 实现 comparison 在排序期间导致了错误。例如, comparison 比较某个项与其自身时可能不会返回 0。

public void Sort(Comparison<T> comparison);

//

// 摘要:

// 使用默认比较器对整个 System.Collections.Generic.List`1 中的元素进行排序。

//

// 异常:

// T:System.InvalidOperationException:

// 默认比较器 System.Collections.Generic.Comparer`1.Default 找不到的一种实现 System.IComparable`1

// 泛型接口或 System.IComparable 类型的接口 T。

public void Sort();

//

// 摘要:

// 使用指定的比较器对整个 System.Collections.Generic.List`1 中的元素进行排序。

//

// 参数:

// comparer:

// 比较元素时要使用的 System.Collections.Generic.IComparer`1 实现,若要使用默认比较器 System.Collections.Generic.Comparer`1.Default,则为

// null。

//

// 异常:

// T:System.InvalidOperationException:

// comparer 是 null, ,和默认比较器 System.Collections.Generic.Comparer`1.Default 找不到实现

// System.IComparable`1 泛型接口或 System.IComparable 类型的接口 T。

//

// T:System.ArgumentException:

// 实现 comparer 在排序期间导致了错误。例如, comparer 比较某个项与其自身时可能不会返回 0。

public void Sort(IComparer<T> comparer);

猜你喜欢

转载自blog.csdn.net/shaynerain/article/details/79129145