property-sort

Sort an array by its property

Usage no npm install needed!

<script type="module">
  import propertySort from 'https://cdn.skypack.dev/property-sort';
</script>

README

Property Sort

npm

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

sortByProperty (method):

sortByProperty(collection: SortObject<T>, options: SortOptions): T[]
Property Type Description
collection SortObject<T> array to sort
options SortOptions options on sorting

SortOptions (interface):

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

SortDirection (interface)

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

SortObject<T> (type)

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.

LocaleType (type)

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.

SortType (type)

SortType is a type which accepts a string or array of strings, which represents the key you would like to sort the array by.