§ ITPOW >> 文档 >> C#

C# OrderByDescending 与 Sort 哪个改变本值,哪个需要返回?

作者:vkvi 来源:ITPOW(原创) 日期:2021-11-3

OrderByDescending 需要返回

List<int> rowIndexs = new List<int>();
// ...
var items = rowIndexs.OrderByDescending(m => m);

如上 rowIndexs 的排序并没有被改变,因为它不改变本值。

Sort 是改变本值,不需要返回

List<int> rowIndexs = new List<int>();
// ...
Comparison<int> comparsion = new Comparison<int>(delegate(int m, int n)
{
	if (m > n)
	{
		return -1;
	}
	else if (m == n)
	{
		return 0;
	}
	else
	{
		return 1;
	}
});
rowIndexs.Sort(comparsion);

如上 Sort 后,rowIndexs 内部的排序得到了变更。

相关文章