Accessing functions from Java Generics

If you intend to sort() your arrays or collections by some numerical value, you can make your classes implements DoubleSupplier:

import java.util.function.DoubleSupplier;

class SomeClass implements DoubleSupplier {
  float x;

  @Override double getAsDouble() {
    return x;
  }
}

And use the Comparator below in order to sort() by ascending numerical order:

import java.util.Comparator;
import java.util.function.DoubleSupplier;

import java.util.Arrays;
import java.util.Collections;

static final Comparator<DoubleSupplier> ASCENT = new Comparator<DoubleSupplier>() {
  @Override final int compare(final DoubleSupplier d1, final DoubleSupplier d2) {
    return (int) Math.signum(d1.getAsDouble() - d2.getAsDouble());
  }
};
  1. Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#sort(T[],java.util.Comparator)
  2. Docs.Oracle.com/javase/10/docs/api/java/util/Collections.html#sort(java.util.List,java.util.Comparator)