# Accessing functions from Java Generics

**URL:** <https://discourse.processing.org/t/accessing-functions-from-java-generics/3391>\
**Category:** Coding Questions\
**Created:** [September 9, 2018, 12:29pm UTC](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391 "2018-09-09T12:29:26Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 9, 2018, 1:37pm UTC](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391/2 "2018-09-09T13:37:16Z")

</div>

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

```auto
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:

```java
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)](http://Docs.Oracle.com/javase/10/docs/api/java/util/Arrays.html#sort(T%5B%5D,java.util.Comparator))
2. [Docs.Oracle.com/javase/10/docs/api/java/util/Collections.html#sort(java.util.List,java.util.Comparator)](http://Docs.Oracle.com/javase/10/docs/api/java/util/Collections.html#sort(java.util.List,java.util.Comparator))

---

_[View the full topic](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391)._
