I am using Linq and Lambda Operations in my projects and I needed to sort a List according to two properties of a class. Therefore, I used OrderBy().ThenBy() methods as below:
class ValueWithIndex{
public long Value;
public int Index;
}
...
List<ValueWithIndex> valuesWithIndex = new List<ValueWithIndex>();
for (int i = 0; i < A.Length; i++){
ValueWithIndex vi = new ValueWithIndex();
vi.Value = A[i];
vi.Index = i;
valuesWithIndex.Add(vi);
}
var orderedValues = valuesWithIndex.OrderBy(v => v.Value).ThenBy(v => v.Index);
...
In This answer, it is explained that, OrderBy() uses Quicksort, so even if it has O(N*logN) average time complexity, for the worst case, time complexity is around O(N2). If also ThenBy() method has a worst time complexity of O(N2), it would be pointless to use these methods.
Is ThenBy() method use Quicksort, too? If yes, does it mean that for the same "v.Value"s, "v.Index"es are sorted with O(N2) worst time complexity?