Rule Definition
Because IOrderedEnumerable inherits from IEnumerable, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy, or ThenByDescending.
Doing this introduces a new primary ordering that ignores the previously established ordering.
This will lead to unexpected behavior as the previous ordering is not followed.
Remediation
Use ThenBy instead of subsequent OrderBy.
Violation Code Sample
var x = personList
.OrderBy(pet => pet.Age)
.OrderBy(pet => pet.type) // Noncompliant
.ToList(); // x is sorted by Name, not sub-sorted
______________________________________________________
var x = personList
.OrderBy(pet => pet.Age)
.ThenBy(pet => pet.type)
.OrderBy(pet => pet.height) // Noncompliant
.ToList(); // x is sorted by height, not sub-sorted
Fixed Code Sample
var x = personList
.OrderBy(pet => pet.Age)
.ThenBy(pet => pet.Name)
.ToList();
Reference
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.orderby?view=netcore-3.1
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.thenby?view=netcore-3.1
Related Technologies
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.