org.springframework.core.annotation

## Class AnnotationAwareOrderComparator

- java.lang.Object
- - org.springframework.core.OrderComparator
  - - org.springframework.core.annotation.AnnotationAwareOrderComparator

- All Implemented Interfaces:
    Comparator<Object>

      public class AnnotationAwareOrderComparator
      extends OrderComparator

  `Comparator` implementation that checks `Ordered` as well as the `Order` annotation, with an order value provided by an `Ordered` instance overriding a statically defined annotation value (if any).
  Since:
    2.0.1
  Author:
    Juergen Hoeller, Oliver Gierke
  See Also:
    `Ordered`, `Order`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static AnnotationAwareOrderComparator` | `INSTANCE` Shared default instance of AnnotationAwareOrderComparator.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AnnotationAwareOrderComparator()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected int` | `getOrder(Object obj)` Determine the order value for the given object.
    `static void` | `sort(List<?> list)` Sort the given List with a default AnnotationAwareOrderComparator.
    `static void` | `sort(Object[] array)` Sort the given array with a default AnnotationAwareOrderComparator.
    `static void` | `sortIfNecessary(Object value)` Sort the given array or List with a default AnnotationAwareOrderComparator, if necessary.

    - ### Methods inherited from class org.springframework.core.OrderComparator

      `compare`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface java.util.Comparator

      `comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- - ### Field Detail

    - #### INSTANCE

          public static final AnnotationAwareOrderComparator INSTANCE

      Shared default instance of AnnotationAwareOrderComparator.

  - ### Constructor Detail

    - #### AnnotationAwareOrderComparator

          public AnnotationAwareOrderComparator()

  - ### Method Detail

    - #### getOrder

          protected int getOrder(Object obj)

      Description copied from class: `OrderComparator`
      Determine the order value for the given object.

      The default implementation checks against the `Ordered` interface. Can be overridden in subclasses.

      Overrides:
        `getOrder` in class `OrderComparator`
      Parameters:
        `obj` - the object to check
      Returns:
        the order value, or `Ordered.LOWEST_PRECEDENCE` as fallback

    - #### sort

          public static void sort(List<?> list)

      Sort the given List with a default AnnotationAwareOrderComparator.

      Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.

      Parameters:
        `list` - the List to sort
      See Also:
        `Collections.sort(java.util.List, java.util.Comparator)`

    - #### sort

          public static void sort(Object[] array)

      Sort the given array with a default AnnotationAwareOrderComparator.

      Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.

      Parameters:
        `array` - the array to sort
      See Also:
        `Arrays.sort(Object[], java.util.Comparator)`

    - #### sortIfNecessary

          public static void sortIfNecessary(Object value)

      Sort the given array or List with a default AnnotationAwareOrderComparator, if necessary. Simply skips sorting when given any other value.

      Optimized to skip sorting for lists with size 0 or 1, in order to avoid unnecessary array extraction.

      Parameters:
        `value` - the array or List to sort
      See Also:
        `Arrays.sort(Object[], java.util.Comparator)`