/// <summary> /// Sorts the vector into ascending order, according to the <i>natural ordering</i>. /// </summary> /// <param name="vector"> /// The vector to be sorted. /// </param> /// <returns> /// A new sorted vector (matrix) viewd /// </returns> public DoubleMatrix1D Sort(DoubleMatrix1D vector) { var indexes = new int[vector.Size]; // row indexes to reorder instead of matrix itself for (int i = indexes.Length; --i >= 0;) { indexes[i] = i; } RunSort( indexes, 0, indexes.Length, (a, b) => { double av = vector[a]; double bv = vector[b]; if (Double.IsNaN(av) || Double.IsNaN(bv)) { return(CompareNaN(av, bv)); // swap NaNs to the end } return(av < bv ? -1 : (av == bv ? 0 : 1)); }); return(vector.ViewSelection(indexes)); }
/// <summary> /// Sorts the vector into ascending order, according to the order induced by the specified comparator. /// </summary> /// <param name="vector"> /// The vector to be sorted. /// </param> /// <param name="c"> /// The comparator to determine the order. /// </param> /// <returns> /// A new matrix view sorted as specified. /// </returns> public DoubleMatrix1D Sort(DoubleMatrix1D vector, DoubleComparator c) { var indexes = new int[vector.Size]; // row indexes to reorder instead of matrix itself for (int i = indexes.Length; --i >= 0;) { indexes[i] = i; } RunSort(indexes, 0, indexes.Length, (a, b) => c(vector[a], vector[b])); return(vector.ViewSelection(indexes)); }