Asked by:
Difference between Linq order by & Linq Sort

Question
-
User-1256377279 posted
Hi All,
Can anyone let me know what difference between Linq order by & Linq Sort and why we used <g class="gr_ gr_122 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation multiReplace" id="122" data-gr-id="122">it.</g>
Below is <g class="gr_ gr_132 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="132" data-gr-id="132">example</g> for Orderby but how we can use the Sort
static void Main(string[] args) { string[] words = { "Blue", "Orange", "Banana", "Apple", "Apricot", "Annn" }; var wordGroups = from w in words orderby w[0] descending group w by w[0]; foreach (var wordgrp in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", wordgrp.Key ); foreach (var word in wordgrp) { Console.WriteLine(word); Console.ReadLine(); } } }
Many Thanks,
Shabbir
Thursday, July 12, 2018 3:53 PM
All replies
-
User1120430333 posted
https://weblogs.asp.net/nmarun/sort-method-versus-orderby-linq-extension-method
Thursday, July 12, 2018 3:59 PM -
User-1256377279 posted
<g class="gr_ gr_5 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="5" data-gr-id="5">Thanks</g> DA
Just want to ask why we use Sort and <g class="gr_ gr_73 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="73" data-gr-id="73"><g class="gr_ gr_72 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" id="72" data-gr-id="72">orderby</g> .</g> What are the main differences?
Shabbir
Thursday, July 12, 2018 4:21 PM -
User1120430333 posted
Thanks DA
Just want to ask why we use Sort and orderby . What are the main differences?
Shabbir
Myself, I would use a Sort on a existing collection, just to arrange the data if needed in ascending or descending order an in -place sort.
names.Sort(a => a.LastName) // assuming there was a LastName property in the Name object in the names collection.
https://www.dotnetperls.com/sort
Orderby would be that I am querying a datasource and I want the results of the query to be ordered, just another way of doing a sort.
https://www.tutorialspoint.com/linq/linq_sorting_operators.htm
Thursday, July 12, 2018 4:55 PM -
User-1256377279 posted
<g class="gr_ gr_5 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="5" data-gr-id="5">Thanks</g> DA.
Any further difference would be appreciated.
Many thanks
Shabbir
Friday, July 13, 2018 8:18 AM -
User1520731567 posted
Hi shabbir_215,
For starters, the LINQ OrderBy is documented as stable (i.e. if two items have the same Name, they'll appear in their original order).
But in Sort(),their order might not be preserved,if two elements are equal.
More detasil:
https://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison
Best Regards.
Yuki Tao
Friday, July 13, 2018 8:51 AM -
User753101303 posted
Hi,
Sort is not part of Linq but a "built-in" method and is a bit more complex to use if you need to customize the ordering. OrderBy is a bit more flexible and if used against a data source it allows to run the ordering on the db server side.
Here as you are also grouping it seems to me using Linq is fine (Sort does nothing else than sorting).
Friday, July 13, 2018 11:16 AM -
User-369506445 posted
as PatriceSe said Sort is not part of Linq and OrderBy is a bit more flexible and if used against a data source it allows to run the ordering on the db server side.
List.Sort uses Array.Sort, which uses the QuickSort algorithm except in a few special cases as the official documentation on MSDN mentions:https://msdn.microsoft.com/en-us/library/w56d4y5z.aspx
OrderBy is a LINQ method that performs a stable sort using deferred execution and a specified comparer (or the default one if no comparer is specified) as the documentation on MSDN also mentions:https://msdn.microsoft.com/en-us/library/bb549422.aspx
Exactly how these methods are implemented are implementation details that are hidden from the consumer, i.e. you don't really need to know this to be able to use the methods and you shouldn't rely on any undocumented implemenation details as these may change in future versions of .NET Framework.
Friday, July 13, 2018 11:52 AM -
User-1171043462 posted
OrderBy() sorts and gives the view
IEnumerable(). But underlying list
is sorted or not changed.
Sort() modifies the underlying
list.Also it seems performance wise too they are different
https://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison
Friday, July 13, 2018 1:57 PM