Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order By vs Sort for creating ordered collection

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.

like image 655
Yogesh Avatar asked May 16 '13 08:05

Yogesh


People also ask

What is the difference between order by and sort by?

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.

What is the difference between sorted and ordered collection?

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.

What is difference between sorted and ordered collection in Hibernate?

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.

What is an ordered collection?

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.


1 Answers

  • 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.
like image 104
Daniel Hilgarth Avatar answered Oct 06 '22 22:10

Daniel Hilgarth