Question: I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator:

```
private static class NaturalComparator<T extends Comparable<? super T>> implements Comparator<T> {
    @Override
    public int compare(T o1, T o2) {
        return o1.compareTo(o2);
    }
}

```

Seems simple enough, but I was wondering if anyone knew of one in the standard API. I looked at TreeMap, and it does it without such a class, so when that code was written, the apparent answer would be no, but perhaps it was added later.

Answer: 
Added to [Comparator in Java 8](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#naturalOrder--):

```
static <T extends Comparable<? super T>> Comparator<T> naturalOrder()

```

Use it like this, for example:

```
Comparator<Double> natural = Comparator.<Double>naturalOrder();
return natural.compare(1.0, 1.1));

```
JDK does not have it, however it is called **ComparableComparator** and it exists in many frameworks such as [Spring](http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/util/comparator/ComparableComparator.html), [Apache Commons](http://commons.apache.org/collections/apidocs/org/apache/commons/collections/comparators/ComparableComparator.html), [Hibernate](http://www.jarvana.com/jarvana/view/org/hibernate/hibernate-core/3.3.1.GA/hibernate-core-3.3.1.GA-javadoc.jar!/org/hibernate/util/ComparableComparator.html) and many others