Skip to content

Order vs Sort in C# #156

Answered by christiannagel
ShervanN asked this question in Q&A
Discussion options

You must be logged in to vote

Order is a LINQ extension method from the Enumerable class available since .NET 7. Because this method extends IEnumerable<T>, you can use the Order method with any collection class implementing IEnumerable<T>. The Order method returns a new collection and doesn't change the source.

The Sort method is defined with List<T>. This method is optimized for theList<T> type and changes the sort order of the originating collection.

The Array type implements a Sort method as well.

That said, use the Order method with any collection type, when you just have IEnumerable<T> available. If you use the List type sorted, use the Sort method if you want to have the original collection sorted. In case you …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ShervanN
Comment options

Answer selected by christiannagel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants