README
Property Sort
property-sort
provides a method that lets you setup sorting in alphabetical, numerical ordering and more. No more hassle sorting your arrays.
Getting started
Install the package...
npm install property-sort
Usage
Plain TypeScript:
import sortByProperty, { SortDirections, SortOptions, SortObject } from 'property-sort';
import LanguageCode from 'language-code';
...
let collection: SortObject<YourItem[]> = ...;
let options: SortOptions = {
direction: SortDirections.Ascending,
sortKey: ['adress', 'suite'],
locale: LanguageCode.en,
numeric: true
};
collection = sortByProperty(collection, options);
Properties
(method): sortByProperty
sortByProperty(collection: SortObject<T>, options: SortOptions): T[]
Property | Type | Description |
---|---|---|
collection | SortObject<T> |
array to sort |
options | SortOptions | options on sorting |
(interface): SortOptions
Property | Type | Description |
---|---|---|
sortKey | SortType | Key to sort the array by |
direction | SortDirection | Sets sort to ascending/descending order |
locale? | LocaleType | Used to compare in chosen locale |
numeric? | boolean |
Used when comparing strings using numeric values |
(interface) SortDirection
Key | Value | Description |
---|---|---|
None | 0 | Used to reset sorting |
Ascending | 1 | Used to sort in ascending order |
Descending | 2 | Used to sort in descending order |
(type) SortObject<T>
SortObject
is a generic type, which takes in an array of unknown objects. This will allow you to to work with your own interfaces while benefitting from writing typed.
(type) LocaleType
A standardized nomenclature used to classify languages. The LocaleType
accepts a wildcard string or an ENUM using LanguageCode
. As a fallback LocaleType
will be set to "en" (English), this will be done when leaving it as undefined or an invalid LocaleType
.
(type) SortType
SortType
is a type which accepts a string or array of strings, which represents the key you would like to sort the array by.