JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q35336F
  • License MIT

Orders an array alphabetically. Optional callback allows you to choose what value in each element to sort by

Package Exports

  • @writetome51/alphabetize

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@writetome51/alphabetize) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

alphabetize(
     arr: any[],
     getValueToSortBy? = (element) => element
): void

Orders uppercase letters before their lowercase versions, i.e., ['A','a','AA','aa']

alphabetizeInsensitive(
     arr: any[],
     getValueToSortBy? = (element) => element
): void

Orders without giving uppercase letters priority.

Both functions re-order arr in ascending alphabetical order.
They sort using Array.prototype.sort() with this comparison function:
(a, b) => String(getValueToSortBy(a)) < String(getValueToSortBy(b)) ? -1 : 1
Optional callback getValueToSortBy(element) must return anything that
can be alphabetically compared with other values. By default it simply
returns the passed element.

Examples

let arr = ['z', 'Z', 'zz', 'ZZ', 'a', 'A', 'aa', 'AA', 'c', 'C', '013',
    '000', 0, '012', 1, 10, 11, '011', '001'];

alphabetize(arr);
console.log(arr);
/*****************
 [ 0, '000', '001', '011', '012', '013', 1, 10, 11,
  'A', 'a', 'AA', 'aa', 'C', 'c', 'Z', 'z', 'ZZ', 'zz']
*****************/

 arr = ['z', 'Z', 'zz', 'ZZ', 'a', 'A', 'aa', 'AA', 'c', 'C', '013',
    '000', 0, '012', 1, 10, 11, '011', '001'];
alphabetizeInsensitive(arr);
console.log(arr);
/****************
 [ 0, '000', '001', '011', '012', '013', 1, 10, 11,
 'a', 'A', 'aa', 'AA', 'c', 'C', 'z', 'Z', 'zz', 'ZZ']
*****************/

let objects = [{name: 'frank'}, {name: 'harry'}, {name: 'sheena'},
               {name: 'brett'}, {name: 'sam'}];
alphabetize(objects, (obj) => obj.name);
console.log(objects);
/************
 [
    { name: 'brett' },
    { name: 'frank' },
    { name: 'harry' },
    { name: 'sam' },
    { name: 'sheena' }
]
************/

Installation

npm i @writetome51/alphabetize

Loading

import {alphabetize, alphabetizeInsensitive} from '@writetome51/alphabetize';