Class DoubleComparators
java.lang.Object
com.github.tommyettinger.ds.support.sort.DoubleComparators
A class providing static methods and objects that do useful things with
comparators.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA type-specific comparator mimicking the natural order.protected static classprotected static classA type-specific comparator mimicking the opposite of the natural order.protected static class -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DoubleComparatorstatic final DoubleComparatorstatic final DoubleComparatorA DoubleComparator using ascending order with a tolerance for equality of 0.000001. -
Method Summary
Modifier and TypeMethodDescriptionstatic DoubleComparatorasDoubleComparator(Comparator<? super Double> c) Returns a type-specific comparator that is equivalent to the given comparator.static DoubleComparatorReturns a comparator representing the opposite order of the given comparator.static voidsort(double[] items, int from, int to, DoubleComparator c) Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.static voidsort(double[] items, DoubleComparator c) Sorts all ofitemsby simply callingsort(double[], int, int, DoubleComparator), settingfromandtoso the whole array is sorted.static DoubleComparatortolerantComparator(double tolerance) Builds a new DoubleComparator, using ascending order with a tolerance for equality oftolerance.
-
Field Details
-
NATURAL_COMPARATOR
-
OPPOSITE_COMPARATOR
-
TOLERANT_COMPARATOR
A DoubleComparator using ascending order with a tolerance for equality of 0.000001.
-
-
Method Details
-
oppositeComparator
Returns a comparator representing the opposite order of the given comparator.- Parameters:
c- a comparator.- Returns:
- a comparator representing the opposite order of
c.
-
asDoubleComparator
Returns a type-specific comparator that is equivalent to the given comparator.- Parameters:
c- a comparator, ornull.- Returns:
- a type-specific comparator representing the order of
c.
-
tolerantComparator
Builds a new DoubleComparator, using ascending order with a tolerance for equality oftolerance. You can call itsDoubleComparator.reversed()method to access a descending-order comparator.- Parameters:
tolerance- how much tolerance is acceptable when comparing two doubles as equal- Returns:
- the new DoubleComparator
-
sort
Sorts all ofitemsby simply callingsort(double[], int, int, DoubleComparator), settingfromandtoso the whole array is sorted.- Parameters:
items- the double array to be sortedc- a DoubleComparator to alter the sort order; if null, the natural order will be used
-
sort
Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The sorting algorithm is an in-place mergesort that is significantly slower than a standard mergesort, as its running time is O(n (log n)2), but it does not allocate additional memory; as a result, it can be used as a generic sorting algorithm.
If and only if
cis null, this will delegate toArrays.sort(double[], int, int), which does not have the same guarantees regarding allocation.- Parameters:
items- the double array to be sortedfrom- the index of the first element (inclusive) to be sorted.to- the index of the last element (exclusive) to be sorted.c- a DoubleComparator to alter the sort order; if null, the natural order will be used
-