lexical_sort library
Lexicographic and natural sort comparators for Dart, ported from the lexical_sort Rust crate.
Non-ASCII characters such as á or ß are treated like their closest ASCII
character: á is treated as a, ß is treated as ss, etc.
Lexical comparisons are case-insensitive. Alphanumeric characters are sorted after all other characters (punctuation, whitespace, special characters, emojis, ...).
Natural sorting handles ASCII numbers, e.g. 50 is less than 100. It is
also possible to skip characters that aren't alphanumeric, so e.g. f-5 is
next to f5.
If different strings have the same ASCII representation (e.g. "Foo" and
"fóò"), it falls back to scalar value comparison, so sorting is
deterministic.
NOTE: This library doesn't attempt to be correct for every locale, but it should work reasonably well for a wide range of locales, while providing excellent performance.
Usage
import 'package:lexical_sort/lexical_sort.dart';
final strings = ['ß', 'é', '100', 'hello', 'world', '50', '.', 'B!'];
strings.sort(naturalLexicalCmp);
// ['.', '50', '100', 'B!', 'é', 'hello', 'ß', 'world']
There are eight comparison functions:
| Function | lexicographical | natural | skips non-alphanumeric |
|---|---|---|---|
cmp |
|||
onlyAlnumCmp |
yes | ||
lexicalCmp |
yes | ||
lexicalOnlyAlnumCmp |
yes | yes | |
naturalCmp |
yes | ||
naturalOnlyAlnumCmp |
yes | yes | |
naturalLexicalCmp |
yes | yes | |
naturalLexicalOnlyAlnumCmp |
yes | yes | yes |
Only the lexicographical functions are case-insensitive.
Functions
-
cmp(
String s1, String s2) → int - Compares strings, neither lexicographically nor naturally, without skipping characters.
-
lexicalCmp(
String s1, String s2) → int - Compares strings lexicographically.
-
lexicalOnlyAlnumCmp(
String s1, String s2) → int - Compares strings lexicographically, skipping non-alphanumeric characters.
-
naturalCmp(
String s1, String s2) → int - Compares strings naturally.
-
naturalLexicalCmp(
String s1, String s2) → int - Compares strings naturally and lexicographically.
-
naturalLexicalOnlyAlnumCmp(
String s1, String s2) → int - Compares strings naturally and lexicographically, skipping non-alphanumeric characters.
-
naturalOnlyAlnumCmp(
String s1, String s2) → int - Compares strings naturally, skipping non-alphanumeric characters.
-
onlyAlnumCmp(
String s1, String s2) → int - Compares strings, skipping non-alphanumeric characters.