Skip to content

AronssonFredrik/property-sort

Repository files navigation

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.

Example

let collection = [
  {
    user: { name: "Denzil Norris", age: 22 }
  },
  {
    user: { name: "Clarissa London", age: 20 }
  }
];

let options = {
  direction: SortDirections.Ascending,
  sortKey: ['user', 'name'],
  locale: LanguageCode.en
};

sortByProperty(collection, options);
/*[
  {
    user: { name: "Clarissa London", age: 20 }
  },
  {
    user: { name: "Denzil Norris", age: 22 }
  }
]*/

options = {
  direction: SortDirections.Descending,
  sortKey: ['user', 'age'],
  locale: LanguageCode.en
};

sortByProperty(collection, options);
/*[
  {
    user: { name: "Denzil Norris", age: 22 }
  },
  {
    user: { name: "Clarissa London", age: 20 }
  }
]*/

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: 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. Example: 'key' or following, if the key is in an a nested object: ['nested','key'].