I have been through this article C# Sort and OrderBy comparison
But the answer accepted in this question is obscure to me.
I can't decide when I should use Sort or when I should use OrderBy.
Is there anyway to recommend sort or order by over each other.
So, please give me a succinct answer without any complication.
The difference between "order by" and "sort by" is that the former guarantees total order in the output while the latter only guarantees ordering of the rows within a reducer. If there are more than one reducer, "sort by" may give partially ordered final results.
An ordered collection means that the elements of the collection have a specific order. The order is independent of the value. A List is an example. A sorted collection means that not only does the collection have order, but the order depends on the value of the element.
When use sorting, Hibernate will load the associated Book entities from the database and use a Java Comparator to sort them in memory. That is not a good approach for huge Sets of entities. Ordering uses an ORDER BY clause in the SQL statement to retrieve the entities in the defined order.
An ordered collection represents a group of objects, known as its elements. Ordered collections allow duplicate elements. This interface is typically used to pass ordered collections around and manipulate them where maximum generality is desired.
Sort
when you want to sort the original list. It performs an in-place sort.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With