Class ObjectComparators
java.lang.Object
com.github.tommyettinger.ds.support.sort.ObjectComparators
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classObjectComparators.NaturalImplicitComparator<T extends Comparable<? super T>>A type-specific comparator mimicking the natural order.protected static classprotected static classObjectComparators.OppositeImplicitComparator<T extends Comparable<? super T>>A type-specific comparator mimicking the opposite of the natural order. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Comparator<T>comparingFloat(com.github.tommyettinger.function.ObjToFloatFunction<? super T> keyExtractor) Accepts a function that extracts afloatsort key from a typeT, and returns aComparator<T>that compares by that sort key.static <T> Comparator<T>oppositeComparator(Comparator<T> c) Returns a comparator representing the opposite order of the given comparator.static <K> voidsort(List<K> items, int from, int to, Comparator<? super K> c) Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.static <K> voidsort(List<K> items, Comparator<? super K> c) Sorts all ofitemsby simply callingsort(List, int, int, Comparator)settingfromandtoso the whole array is sorted.static <K> voidsort(K[] items, int from, int to, Comparator<? super K> c) Sorts the specified range of elements according to the order induced by the specified comparator using mergesort.static <K> voidsort(K[] items, Comparator<? super K> c) Sorts all ofitemsby simply callingsort(List, int, int, Comparator)settingfromandtoso the whole array is sorted.
-
Field Details
-
NATURAL_COMPARATOR
-
OPPOSITE_COMPARATOR
-
-
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.
-
comparingFloat
public static <T> Comparator<T> comparingFloat(com.github.tommyettinger.function.ObjToFloatFunction<? super T> keyExtractor) Accepts a function that extracts afloatsort key from a typeT, and returns aComparator<T>that compares by that sort key.
This is in ObjectComparators and not in FloatComparators because there are similar methods in Comparator, such asComparator.comparingDouble(ToDoubleFunction).
The returned comparator is not serializable, by design. This library intentionally avoids the java.io.Serializable ecosystem because of the serious security issues it near-constantly accrues.- Type Parameters:
T- the type of element to be compared- Parameters:
keyExtractor- the function used to extract the float sort key- Returns:
- a comparator that compares by an extracted key
- Throws:
NullPointerException- if the argument is null
-
sort
Sorts all ofitemsby simply callingsort(List, int, int, Comparator)settingfromandtoso the whole array is sorted.- Parameters:
items- the List to be sortedc- a Comparator 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.
- Parameters:
items- the List 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 Comparator to alter the sort order; if null, the natural order will be used
-
sort
Sorts all ofitemsby simply callingsort(List, int, int, Comparator)settingfromandtoso the whole array is sorted.- Parameters:
items- the List to be sortedc- a Comparator 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.
- Parameters:
items- the List 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 Comparator to alter the sort order; if null, the natural order will be used
-