public static Matrix Sort(Matrix source, Func<Vector, double> keySelector, VectorType t, bool ascending, out Vector indices)
{
int max = (t == VectorType.Row ? source.Rows : source.Cols);
indices = Vector.Zeros(max);
List<Vector> vects = new List<Vector>(max);
IEnumerable<Vector> arrays = (t == VectorType.Row ? source.GetRows() : source.GetCols());
KeyValuePair<Vector, int>[] sort = (ascending ?
arrays.Select((i, v) => new KeyValuePair<Vector, int>(i, v))
.OrderBy(o => keySelector(o.Key))
:
arrays.Select((i, v) => new KeyValuePair<Vector, int>(i, v))
.OrderByDescending(o => keySelector(o.Key))).ToArray();
indices = sort.Select(s => s.Value).ToVector();
return sort.Select(s => s.Key).ToMatrix(t);
}