AlphanumericComparator

A comparator that sorts alphanumeric strings in a human-friendly way, also known as natural sort: runs of digits are compared by their numeric value instead of character by character. For example, $120 comes after $30, as opposed to the usual lexicographical order.

Strings are split into chunks of consecutive digits or non-digits, compared pairwise:

  • Two digit chunks are compared by numeric value. Equal values with different zero-padding (e.g. 7 vs 07) are only ordered, fewer leading zeros first, when the strings are otherwise equal.

  • Any other pair of chunks is compared lexicographically.

This is a multiplatform port of sawano/alphanumeric-comparator.

Functions

Link copied to clipboard
open override fun compare(a: CharSequence, b: CharSequence): Int
Link copied to clipboard
Link copied to clipboard
open fun <U : Comparable<in U?>?> thenComparing(keyExtractor: Function<in CharSequence?, out U?>?): Comparator<CharSequence?>?
open fun <U : Any?> thenComparing(keyExtractor: Function<in CharSequence?, out U?>?, keyComparator: Comparator<in U?>?): Comparator<CharSequence?>?
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard