9

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.

Community
  • 1
  • 1
Yogesh
  • 3,044
  • 8
  • 33
  • 60
  • 1
    The answer on the linked question is quite clear: It doesn't matter for speed. Use whatever is most convenient. – H H May 16 '13 at 08:41

1 Answers1

25
  • 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.
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 5
    I'd add "Use `OrderBy` when you need a [Stable Sort](http://en.wikipedia.org/wiki/Stable_sort#Stability) – Matthew Watson May 16 '13 at 08:42
  • 2
    Also, OrderBy returns a IOrderedEnumerable, and you can chain other LINQ functions after it (for example, .Select(p => ....)) – icesar May 16 '13 at 08:44
  • 2
    @MatthewWatson: Good point. I wasn't aware of that difference but the MSDN confirms it. `Sort` performs an unstable sort, `OrderBy` performs a stable sort. – Daniel Hilgarth May 16 '13 at 08:45
  • @DanielHilgarth, I have a custom collection class which inherits from ICollection.Right before i sort the collection i need to check for invalid entities in the collection, therefore i use where clause and then i use sort since where clause gives new collection rather so sorting is performed on different collection rather than on main collection object, Code tends to be like m_objUndoRedoObservableCollection.Where(obj=>obj.isValid).sort(custom comparer i pass) so is there any way to do sort on the main collecton with the help of where and sort – Yogesh May 16 '13 at 09:03
  • @YogeshJoshi: The code you showed in your comment won't compile. There is no `sort` or `Sort` defined for `IEnumerable`. To answer the question: You can simply do this: `m_objUndoRedoObservableCollection = new ObservableCollection(m_objUndoRedoObservableCollection.Where(obj=>obj.isValid).OrderBy(...));` – Daniel Hilgarth May 16 '13 at 09:04
  • @DanielHilgarth MyMistake( yes that is right we have to use ToList() before calling sort()) – Yogesh May 16 '13 at 09:05
  • @DanielHilgarth i want in-place sorting therefore i can't use order by. i want to use sort with where is that possible anyhow. – Yogesh May 16 '13 at 09:06
  • @YogeshJoshi: In that case the code could look like this: `m_objUndoRedoObservableCollection = new ObservableCollection(m_objUndoRedoObservableCollection.Where(obj=>obj.‌​isValid).ToList().Sort());`. However, in this scenario it is better to use `OrderBy` because when using `Sort` - which requires `ToList` - you copy the objects twice. Once into the temporary list you sort on and once into the new observable collection. – Daniel Hilgarth May 16 '13 at 09:07
  • @YogeshJoshi: You can't perform an in-place sort, because you want to also perform a filtering. – Daniel Hilgarth May 16 '13 at 09:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30043/discussion-between-yogesh-joshi-and-daniel-hilgarth) – Yogesh May 16 '13 at 09:08
  • @YogeshJoshi: I am not available for chat. If you have further questions, post them as such. This is already getting way out of hand here in the comments. – Daniel Hilgarth May 16 '13 at 09:09