I think it's important to note another difference between Sort
and OrderBy
:
Suppose there exists a Person.CalculateSalary()
method, which takes a lot of time; possibly more than even the operation of sorting a large list.
Compare
// Option 1
persons.Sort((p1, p2) => Compare(p1.CalculateSalary(), p2.CalculateSalary()));
// Option 2
var query = persons.OrderBy(p => p.CalculateSalary());
Option 2 may have superior performance, because it only calls the CalculateSalary
method n times, whereas the Sort
option might call CalculateSalary
up to 2n log(n) times, depending on the sort algorithm's success.