Class CharSequenceComparator

  • All Implemented Interfaces:
    java.util.Comparator<java.lang.CharSequence>

    public class CharSequenceComparator
    extends java.lang.Object
    implements java.util.Comparator<java.lang.CharSequence>
    A Comparator implementation for comparing CharSequence objects.

    This class provides a consistent and null-safe comparison mechanism for CharSequence instances, primarily designed to be used in sorting and ordering operations.

    Example Usage

    
     List<CharSequence> sequences = Arrays.asList("apple", "banana", "apple", null);
     Collections.sort(sequences, CharSequenceComparator.INSTANCE);
     

    The comparison is based on lexicographical ordering similar to String.compareTo(String). Null values are considered smaller than non-null values. When both sequences are null, they are considered equal.

    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.CharSequence c1, java.lang.CharSequence c2)
      Compares two CharSequence instances lexicographically.
      • 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

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Method Detail

      • compare

        public int compare​(java.lang.CharSequence c1,
                           java.lang.CharSequence c2)
        Compares two CharSequence instances lexicographically. Null values are considered less than non-null values. Two null values are considered equal.

        Example Usage

        
           int result = CharSequenceComparator.INSTANCE.compare("apple", "banana"); // negative
           int equal = CharSequenceComparator.INSTANCE.compare("same", "same");     // 0
           int nullFirst = CharSequenceComparator.INSTANCE.compare(null, "text");   // negative
         
        Specified by:
        compare in interface java.util.Comparator<java.lang.CharSequence>
        Parameters:
        c1 - the first CharSequence to compare; may be null
        c2 - the second CharSequence to compare; may be null
        Returns:
        a negative integer, zero, or a positive integer as c1 is less than, equal to, or greater than c2
        Since:
        1.0.0