use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*Sorts the vector into ascending order, according to the order induced by the specified comparator.
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*The algorithm compares two cells at a time, determinining whether one is smaller, equal or larger than the other.
*To sort ranges use sub-ranging views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*<pre>
*// sort by sinus of cells
*ObjectComparator comp = new ObjectComparator() {
* public int compare(Object a, Object b) {
* Object as = Math.sin(a); Object bs = Math.sin(b);
* return as < bs ? -1 : as == bs ? 0 : 1;
* }
*};
*sorted = quickSort(vector,comp);
*</pre>
*
*@param vector the vector to be sorted.
*@param c the comparator to determine the order.
*@return a new matrix view sorted as specified.
* <b>Note that the original vector (matrix) is left unaffected.</b>
*/
public ObjectMatrix1D sort(final ObjectMatrix1D vector, final java.util.Comparator c) {
// row indexes to reorder instead of matrix itself
int[] indexes = new int[vector.size()];
for (int i = indexes.length; --i >= 0; ) indexes[i] = i;
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
return c.compare(vector.getQuick(a), vector.getQuick(b));
}
};
runSort(indexes, 0, indexes.length, comp);
return vector.viewSelection(indexes);
}
Aggregations