You can implement IComparer<List<int>>
and use it with LINQ's OrderBy
method:
public class ListComparer : IComparer<List<int>> {
public int Compare(List<int> x, List<int> y) // > 1, == 0, < -1
=> x.Zip(y, (x1, y1) => (x1,y1))
.Where(xy => xy.x1 != xy.y1)
.Select(xy => xy.x1 > xy.y1 ? 1 : -1)
.DefaultIfEmpty(x.Count > y.Count ? 1 : x.Count == y.Count ? 0 : -1)
.First();
}
NOTE: If a list is a prefix of another the list, the shorter list is sorted first. If two lists are equal, they remain in their original order.
With this, you can
var ans = src.OrderBy(x => x, new ListComparer());
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…