If I've understood what you're trying to do, you need to implement a custom
IComparer<T>[
^] to sort the list:
public sealed class ListComparer<T> : IComparer<IReadOnlyList<T>> where T : IComparable
{
public int Compare(IReadOnlyList<T> left, IReadOnlyList<T> right)
{
if (left is null) return right is null ? 0 : -1;
if (right is null) return 1;
var innerComparer = Comparer<T>.Default;
int count = Math.Min(left.Count, right.Count);
for (int index = 0; index < count; index++)
{
int result = innerComparer.Compare(left[index], right[index]);
if (result != 0) return result;
}
return left.Count.CompareTo(right.Count);
}
}
With that in place, it's then easy to sort your list:
var list = new List<List<int>>
{
new List<int> { 5, 9, 17, 40, 99 },
new List<int> { 12, 24, 30, 45, 80 },
new List<int> { 5, 9, 17, 16, 99 },
new List<int> { 1, 5, 8, 15, 25 }
};
list.Sort(new ListComparer<int>());
var sortedList = list.OrderBy(l => l, new ListComparer<int>()).ToList();
The output in either case matches the desired output from your question.
This comparer will cope with lists of different lengths; if one list is equal to the other list with some extra elements appended, the longer list will be sorted after the shorter one.