Class ObjectComparators

java.lang.Object
com.github.tommyettinger.ds.support.sort.ObjectComparators

public final class ObjectComparators extends Object
  • Field Details

    • NATURAL_COMPARATOR

      public static final Comparator<?> NATURAL_COMPARATOR
    • OPPOSITE_COMPARATOR

      public static final Comparator<?> OPPOSITE_COMPARATOR
  • Method Details

    • oppositeComparator

      public static <T> Comparator<T> oppositeComparator(Comparator<T> c)
      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 a float sort key from a type T, and returns a Comparator<T> that compares by that sort key.
      This is in ObjectComparators and not in FloatComparators because there are similar methods in Comparator, such as Comparator.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

      public static <K> void sort(List<K> items, Comparator<? super K> c)
      Sorts all of items by simply calling sort(List, int, int, Comparator) setting from and to so the whole array is sorted.
      Parameters:
      items - the List to be sorted
      c - a Comparator to alter the sort order; if null, the natural order will be used
    • sort

      public static <K> void sort(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.

      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 sorted
      from - 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

      public static <K> void sort(K[] items, Comparator<? super K> c)
      Sorts all of items by simply calling sort(List, int, int, Comparator) setting from and to so the whole array is sorted.
      Parameters:
      items - the List to be sorted
      c - a Comparator to alter the sort order; if null, the natural order will be used
    • sort

      public static <K> void sort(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.

      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 sorted
      from - 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