You will need a row comparer. We’ll keep in mind that all rows have same length:
public class RowComparer : IComparer<IEnumerable<int>>
{
public int Compare(IEnumerable<int> x, IEnumerable<int> y)
{
// TODO: throw ArgumentNullException
return x.Zip(y, (xItem, yItem) => xItem.CompareTo(yItem))
.Where(c => c != 0).FirstOrDefault();
}
}
And use it for sorting:
inputRows.Select(r => r.OrderBy(x => x)).OrderBy(r => r, new RowComparer())
Output for your sample:
[
[ 1, 2 ],
[ 2, 2 ],
[ 2, 3 ]
]
4
solved How to sort matrix column values then rows?