
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
alphabetic-compare
Advanced tools
Checks whether two strings are in alphabetical order, with support for natural sort and national quirks
Checks whether two strings are in alphabetical order, with support for natural sort and national quirks.
import compare from 'alphabetic-compare';
['Alan 10', 'Bobby', 'Alan 2', 'Bob Smith'].sort((a,b) => compare(a, b, 'en'));
// → [Alan2, Alan10, Bob Smith, Bobby]
Keywords: Alphabetical order, alphabetization, lexicographical order, language-specific ordering conventions, collation, sorting, numeric order.
To compare strings in supported languages, you can use this method:
function compare(a: string, b: string, lang: ISO_639_1): -1 | 0 | 1
If string a
should be sorted above b
, you get -1
. If it sould be sorted under, you get 1
. 0
means the strings are alphabetically identical, but they might still differ in non-alphabetical symbols like the apostrophe.
If you want to customize the sorting algorithm, instead of language you can pass settings to the method:
interface Configuration {
language?: ISO_639_1,
nullSorting?: -1 | 0 | 1,
allowIntl?: 0 | 1 | 2,
customSorting?: Sorting
}
function compare(a: string, b: string, conf: Configuration): -1 | 0 | 1
Null sorting lets you decide what happens with unknown symbols. With -1
they will be sorted above everything else, with 1
they will be sorted under everything else and with 0
(default) they will be ignored.
Intl fallback can be controlled by allowIntl
. Value 0
means it won't be ever used, 1
(default) means use Intl if there's no local rule for the language, 2
means use Intl whenever it's possible.
To find out how to define your own sorting, take a look at test/basic.ts
and src/lang.ts
. If pass the sorting as customSorting
, the selected language will be ignored.
Intl
FAQs
Checks whether two strings are in alphabetical order, with support for natural sort and national quirks
We found that alphabetic-compare demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.