Class Comparisons


  • public final class Comparisons
    extends Object
    Comparison utility methods.
    Author:
    Curtis Rueden, Gabriel Selzer
    • Method Detail

      • compare

        public static <T extends Comparable<? super T>> int compare​(T o1,
                                                                    T o2)
        Compares two comparable objects, even if one or both of them are null.

        By convention, nulls are considered less than non-nulls (and will hence will appear first on sorted lists).

        Parameters:
        o1 - The first object to compare.
        o2 - The second object to compare.
        Returns:
        The result of o1.compareTo(o2) unless one or both of them is null, in which case nulls are considered less than non-null objects.
      • compare

        public static <T extends Comparable<? super T>> int compare​(T o1,
                                                                    T o2,
                                                                    boolean heavyNulls)
        Compares two comparable objects, even if one or both of them are null.
        Parameters:
        o1 - The first object to compare.
        o2 - The second object to compare.
        heavyNulls - If true, nulls will be treated as greater than non-nulls, and hence "sink to the bottom" of e.g. sorted lists.
        Returns:
        The result of o1.compareTo(o2) unless one or both of them is null, in which case the null strategy defined by the heavyNulls flag is used, to define nulls as either less than, or greater than, non-null objects.