Socket
Book a DemoInstallSign in
Socket

alphabetic-compare

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alphabetic-compare

Checks whether two strings are in alphabetical order, with support for natural sort and national quirks

latest
npmnpm
Version
1.1.4
Version published
Maintainers
1
Created
Source

alphabetic-compare

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]

Features

  • Natural sorting of numbers
  • Multiword sorting
  • Internationalized sorting for different languages
  • Independent of Intl
  • Intl can be used as fallback
  • Custom sorting rules

Keywords: Alphabetical order, alphabetization, lexicographical order, language-specific ordering conventions, collation, sorting, numeric order.

Basic usage

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.

Advanced usage

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.

Supported languages

  • English
  • Czech
  • Slovak
  • all languages made available by Intl

Keywords

alphabetical order

FAQs

Package last updated on 09 May 2019

Did you know?

Socket

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.

Install

Related posts