Crates.io | lexical-sort |
lib.rs | lexical-sort |
version | 0.3.1 |
source | src |
created_at | 2020-06-15 03:08:27.179459 |
updated_at | 2020-08-29 23:40:26.626168 |
description | Sort Unicode strings lexically |
homepage | https://lib.rs/crates/lexical-sort |
repository | https://github.com/Aloso/lexical-sort |
max_upload_size | |
id | 254058 |
size | 113,284 |
This is a library to compare and sort strings (or file paths) lexicographically. This means that 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, ...).
It is possible to enable natural sorting, which also handles ASCII numbers. For example, 50
is less than 100
with natural sorting turned on. It's 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 the default method from the standard library, so sorting is deterministic.
NOTE: This crate 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. |
To sort strings or paths, you can use the StringSort
or StringSort
trait:
use lexical_sort::{StringSort, natural_lexical_cmp};
let mut strings = vec!["ß", "é", "100", "hello", "world", "50", ".", "B!"];
strings.string_sort_unstable(natural_lexical_cmp);
assert_eq!(&strings, &[".", "50", "100", "B!", "é", "hello", "ß", "world"]);
There are eight comparison functions:
Function | lexicographical | natural | skips non-alphanumeric chars |
---|---|---|---|
cmp |
|||
only_alnum_cmp |
yes | ||
lexical_cmp |
yes | ||
lexical_only_alnum_cmp |
yes | yes | |
natural_cmp |
yes | ||
natural_only_alnum_cmp |
yes | yes | |
natural_lexical_cmp |
yes | yes | |
natural_lexical_only_alnum_cmp |
yes | yes | yes |
Note that only the functions that sort lexicographically are case insensitive.
All comparison functions constitute a total order. Two strings are only considered equal if they consist of exactly the same Unicode code points.
The algorithm uses iterators and never allocates memory on the heap. It is optimized for strings that consist mostly of ASCII characters; for ASCII-only strings, the lexicographical comparison functions are only 2 to 3 times as slow as the default method from std, which just compares Unicode code points.
Note that comparisons are slower for strings where many characters at the start are the same (after transliterating them to lowercase ASCII).
These benchmarks were executed on an AMD A8-7600 Radeon R7 CPU with 4x 3.1GHz.
"T-"
followed by 1 to 8 decimal digits. This is a stress test for natural sorting.The first, grey bar is from the rust_icu
crate that provides bindings to the icu C library. This performs "proper" collation for English. It is faster in many cases, because it generates search keys up front to reduce the total amount of work.
The last, dark blue bar is the string comparison function in the standard library.
no_std
supportThis crate supports no_std
environments. Note that you have to disable default features to compile without the standard library.
This crate currently doesn't require an allocator, although this is likely going to change in the future.
Contributions, bug reports and feature requests are welcome!
If support for certain characters is missing, you can contribute them to the any_ascii crate.
This project is dual-licensed under the MIT and Apache 2.0 license. Use whichever you prefer.