Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/writetome51/alphabetize
Function puts array of strings in alphabetical order
https://github.com/writetome51/alphabetize
alphabetical alphabetizer javascript sort
Last synced: 7 days ago
JSON representation
Function puts array of strings in alphabetical order
- Host: GitHub
- URL: https://github.com/writetome51/alphabetize
- Owner: writetome51
- License: mit
- Created: 2018-10-02T08:03:35.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-09-03T04:05:51.000Z (over 4 years ago)
- Last Synced: 2024-11-08T22:39:19.989Z (2 months ago)
- Topics: alphabetical, alphabetizer, javascript, sort
- Language: TypeScript
- Size: 35.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# alphabetize(
arr: any[],
getValueToSortBy? = (element) => element
): voidOrders uppercase letters before their lowercase versions, i.e., `['A','a','AA','aa']`
# alphabetizeInsensitive(
arr: any[],
getValueToSortBy? = (element) => element
): voidOrders 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
```js
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
```js
import {alphabetize, alphabetizeInsensitive} from '@writetome51/alphabetize';
```