Order By vs Sort用于创建有序集合 [英] Order By vs Sort for creating ordered collection

查看:94
本文介绍了Order By vs Sort用于创建有序集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看完这篇文章 C#排序和排序比较

I have been through this article C# Sort and OrderBy comparison

但是这个问题接受的答案对我来说还是很模糊的.

But the answer accepted in this question is obscure to me.

我无法确定何时使用排序或何时使用OrderBy.

无论如何,建议彼此进行排序或排序.

Is there anyway to recommend sort or order by over each other.

所以,请给我一个简洁的答案,没有任何复杂性.

So, please give me a succinct answer without any complication.

推荐答案

  • 要对原始列表进行排序时,请使用Sort.它执行就地排序.
  • 当您不想更改原始列表时,可以使用OrderBy,因为它会返回使原始列表保持不变的IOrderedEnumerable<T>.或者,当您没有列表但还有其他可枚举的列表时.
    • You use Sort when you want to sort the original list. It performs an in-place sort.
    • You use OrderBy when you don't want to change the original list as it returns an IOrderedEnumerable<T> that leaves the original list untouched. Or when you don't have a list but some other enumerable.
    • 这篇关于Order By vs Sort用于创建有序集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆