Skip to content

io microsphere util CharSequenceComparator

github-actions[bot] edited this page Oct 10, 2026 · 21 revisions

CharSequenceComparator

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/CharSequenceComparator.java

Overview

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 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.

Declaration

public class CharSequenceComparator implements Comparator<CharSequence>

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.3.20-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

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

Method Examples

compare

int result = CharSequenceComparator.INSTANCE.compare("apple", "banana"); // negative
  int equal = CharSequenceComparator.INSTANCE.compare("same", "same");     // 0
  int nullFirst = CharSequenceComparator.INSTANCE.compare(null, "text");   // negative

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.CharSequenceComparator;

API Reference

Public Methods

Method Description
compare Compares two CharSequence instances lexicographically.

Method Details

compare

public int compare(@Nullable CharSequence c1, @Nullable 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
`

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

annotation-test

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally