Order vs Sort in C# #156
-
Happy New Year. What are the differences between Sort and Order in C#? When and Where should we use these?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The The That said, use the |
Beta Was this translation helpful? Give feedback.
Order
is a LINQ extension method from the Enumerable class available since .NET 7. Because this method extendsIEnumerable<T>
, you can use theOrder
method with any collection class implementingIEnumerable<T>
. TheOrder
method returns a new collection and doesn't change the source.The
Sort
method is defined withList<T>
. This method is optimized for theList<T>
type and changes the sort order of the originating collection.The
Array
type implements aSort
method as well.That said, use the
Order
method with any collection type, when you just haveIEnumerable<T>
available. If you use the List type sorted, use theSort
method if you want to have the original collection sorted. In case you …