Reputation: 12675
I have a List<> that I've previously sorted by one of the fields on the object. If I perform a Where() on the list, am I safe to assume that the results of the Where will also be sorted?
// Sorted by salary, greatest to smallest.
List<Players> players = _playerRepo.GetAll().OrderByDescending(x => x.Salary).ToList();
// Is this list safe to assume that the players with smith as last name are sorted by salary as well?
List<Players> filteredPlayers = players.Where(x => x.LastName == "Smith").ToList();
Upvotes: 2
Views: 66
Reputation: 107536
Yes, Where
will not change the order of a previously ordered list, it will only filter it.
Also, the ordering operations are stable, so equal values will stay in their original order.
Upvotes: 2
Reputation: 34295
Yes. LINQ queries, in general, keep the order of elements.
Hint: don't call ToList
if you want to filter the result further. You should peform query on the database, not on the client, if possible.
Upvotes: 3