lexical_sort 1.0.2
lexical_sort: ^1.0.2 copied to clipboard
Lexicographic and natural sort comparators for Dart, ported from the lexical_sort Rust crate.
lexical_sort - Lexicographic and natural sort comparators for Dart #
Introduction #
lexical_sort is a string comparison library for Dart, ported from the lexical_sort Rust crate.
It provides lexicographic and natural sort comparators that treat non-ASCII characters like their
closest ASCII equivalent. For example, á is treated as a, ß is treated as ss. Comparisons
are case-insensitive. Alphanumeric characters sort after all other characters.
Natural sorting handles embedded ASCII numbers correctly — 50 sorts before 100.
If different strings have the same ASCII representation (e.g. "Foo" and "fóò"), it falls back
to scalar value comparison, so sorting is always deterministic.
The translation was done using Claude. If you find any bugs or incomplete behavior, feel free to open an issue or raise a PR.
Description #
Lexicographic and natural sort comparators for Dart, ported from the lexical_sort Rust crate.
Installation #
Go to https://pub.dev/packages/lexical_sort for the latest version of lexical_sort
Usage #
import 'package:lexical_sort/lexical_sort.dart';
void main() {
final strings = ['ß', 'é', '100', 'hello', 'world', '50', '.', 'B!'];
strings.sort(naturalLexicalCmp);
print(strings); // [., 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.
Development #
Requirements
Installing Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Verify the installation:
rustc --version
cargo --version
Regenerating the data file
The file lib/src/_alnum_data.dart is generated from Rust's standard library. It is committed to
the repository and does not need to be regenerated unless you want to update to a newer Rust
toolchain version.
To regenerate:
./scripts/generate.sh
This runs the Rust generator at lexical-sort-generate/src/main.rs which iterates all Unicode
codepoints, calls Rust's char::is_alphanumeric() for each, and writes the resulting
Set<int> to lib/src/_alnum_data.dart.
Running tests
dart test
Contacts #
Please feel free to reach out via the repository.
About #
- Package URL: https://pub.dev/packages/lexical_sort
- Repo URL: https://github.com/ganeshrvel/pub_lexical_sort
License #
lexical_sort | Lexicographic and natural sort comparators for Dart. MIT License.