score:7
put simply, he's wrong. the linq orderby
et al. methods are documented as performing a stable sort:
this method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. in contrast, an unstable sort does not preserve the order of elements that have the same key.
score:0
he's saying if do, say orderby lastname
only then anybody with the same last name will be sorted in an unpredictable order. so if you combine it with, say orderby lastname, firstname
you can make the order predictable again (except for people with the same first and last name of course).
at least, that's what i would assume.
score:5
unstable sort means that a chain of x.orderby(...).orderby(...)
calls will only reliably sort according to the final criterion. x.orderby(...).thenby(...)
explicitly captures knowledge of the previous sort order when applying the new sort order. i believe it does this by calling iorderedenumerable<telement>.createorderedenumerable<tkey>
, though i'm not 100% sure of this.
edit: just to be clear, when i say, "captures the knowledge..." i don't mean to suggest that the first orderby performs a sort, and somehow the second one knows what it did. remember that orderby
returns an iorderedenumerable<t>
, which doesn't perform any work at all until someone tries to consume the elements. in this scenario, it will never perform the sort, since thenby
, using knowledge of how orderby
would have sorted, constructs a brand new sorter that applies both sort orderings in the expected manner and in a single step.
it has been pointed out that magennis is wrong on the unstable sort thing. the above description is still valid, however.
Source: stackoverflow.com
Related Query
- LINQ Quicksort is Unstable Except When Cascading
- How to reuse a linq expression for 'Where' when using multiple source tables
- Avoiding code repetition when using LINQ
- LINQ Source Code Available
- LINQ query returns old results when source list is re-initialized
- creating Linq to sqlite dbml from DbLinq source code
- C# - Code supposed to be unreachable when calculate value in LINQ sum method
- Linq sub query when using a repository pattern with EF code first
- why does this linq code get exponentially slower when applying First() to projection?
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How Except in linq works when comparing entities?
- should i use Except or Contains when dealing with HashSet or so in Linq
- source code for LINQ 101 samples
- Linq code have errors when i use Distinct()
- WPF DataGrid is filled except when I use LINQ to filter its items
- c# Linq or code to extract groups from a single list of source data
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- Linq Find Partial Text Match - Included code returns duplicate and everything except what it should
- Getting the Error in my code when framing LINQ
- What does LINQ return when the results are empty
- Convert string[] to int[] in one line of code using LINQ
- When to use Cast() and Oftype() in Linq
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Linq code to select one item
- Filter Linq EXCEPT on properties
- Entity-framework code is slow when using Include() many times
- How are people unit testing code that uses Linq to SQL
- Ambiguous call when using LINQ extension method on DbSet<T>
More Query from same tag
- BETWEEN EQUIVALENT in LINQ
- Linq to SQL, select distinct rows where column is max?
- how to search Year between in Dynamic Linq
- LINQ Grouping dynamically
- Query Excel where Rows and Columns are reversed
- Set Attribute value in LINQ to XML
- How to: Select Distinct objects<T> from multiple Lists<T> with 1 or more properties of same type
- Linq, select differences between two lists, select same between same list, and select duplicates same list
- LINQ Query to Find the Maximum Mean for a Time Span
- Entity Framework Execute Expression
- Tree-based mathematical expressions using objects with assignable Func delegates
- How to load data from XML to List
- On ASP.NET ListView with LinqDataSource, display the data when the search button clicked
- How to avoid composing Where() operators in LINQ?
- LINQ query not taking String as the value
- Binding LINQ query to DataGridView
- problem using foreach in linq query result
- Is NHibernate.Linq 1.0 GA Provider Production Ready
- Fetch value from child's child nodes
- List<T> - distinction by T.field
- How to dereference a string that might be null in LINQ
- How to convert System.Linq.Enumerable.WhereListIterator<int> to List<int>?
- Observable<string> updated events?
- Cast a Typed list<> to be prepared for DataAdapter Update
- Visual Basic LINQ
- How can I get the value of an attribute for a method that has duplicates?
- CosmosDb string search on numeric
- How can I filter a List with linq based on a value of a list within?
- Dynamic Linq Groupby SELECT Key, List<T>
- RX Extension ToObservable() yields an exception when querying a nested IColletion