Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sort-by-property

jvandenaardweg6.9kMIT1.3.0TypeScript support: included

Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.

typescript, sorting, array, sort, sortby, javascript, order, orderby, typed, typesafe

readme

sort-by-property npm version mit license

Type-safe array sorting method with support for deeply nested properties and Typescript autocompletion.

blogPosts.sort(sortByProperty('author.name', 'asc'));

Features

  • Type-safety and Typescript autocompletion on the properties you try to sort
  • Define nested property to sort on using a path string like: "author.name"
  • Supports sorting by string, number, boolean, Date, Symbol and BigInt values
  • Handles null and undefined values gracefully by moving the object to the end of the array
  • Small file size, only 0.6kb gzipped
  • High performance, up to 3 times faster than lodash orderBy and sortBy *

Try it out: https://codesandbox.io/s/sort-by-property-example-hin358

Requirements

Requires Typescript 4.1+ because of the internal use of template literals for the autocompletion.

Installation

Install with your favorite package manager:

npm install sort-by-property

or

yarn add sort-by-property

Usage

// For an array of objects
import { sortByProperty } from 'sort-by-property';

// For one-dimensional arrays
import { sortBy } from 'sort-by-property';

Example: Sorting an array of objects

import { sortByProperty } from 'sort-by-property';

interface BlogPost {
  id: number;
  title: string;
  author: {
    id: number;
    name: string;
  };
}

const blogPosts: BlogPost[] = [
  {
    id: 1,
    title: 'Never gonna run around and desert you',
    author: {
      id: 10,
      name: 'Joe',
    },
  },
  {
    id: 2,
    title: 'Never gonna let you down',
    author: {
      id: 20,
      name: 'Ben',
    },
  },
  {
    id: 3,
    title: 'Never gonna give you up',
    author: {
      id: 30,
      name: 'Alice',
    },
  },
];

// Sort the blog posts by author name
blogPosts.sort(sortByProperty('author.name', 'asc'));

// If you need to use a custom locale for sorting strings, you can do
blogPosts.sort(sortByProperty('author.name', 'asc', {locale: 'nb-no'}))

Will sort the array ascending by author.name:

[
  { id: 3, title: 'Never gonna give you up', author: { id: 30, name: 'Alice' } },
  { id: 2, title: 'Never gonna let you down', author: { id: 20, name: 'Ben' } },
  { id: 1, title: 'Never gonna run around and desert you', author: { id: 10, name: 'Joe' } },
];

Will show a type error when you try to sort on properties that do not exist:

type-error-example

Will show an autocomplete of the available properties to sort on:

autocomplete

Example: Sorting one-dimensional arrays

This package exports 2 methods. Use sortBy to sort one-dimensional arrays. This sorting method supports all the same types as sortByProperty.

import { sortBy } from 'sort-by-property';

const array = ['c', 'b', 'a'];

array.sort(sortBy('asc'));

// Result: ['a', 'b', 'c']

* on an array with 10 million items: ~450ms vs ~1350ms. See the /src/examples directory.

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

1.3.0

  • chore(deps-dev): bump jest and @types/jest 4c2fdae
  • chore(deps-dev): bump jest from 29.4.1 to 29.4.3 a2f51c4
  • chore(deps-dev): bump release-it from 15.6.1 to 15.10.1 ecc1a03

1.2.4

2 January 2023

  • chore(deps-dev): bump @typescript-eslint/eslint-plugin f117a48
  • chore(deps-dev): bump @typescript-eslint/eslint-plugin 1af35ff
  • chore(deps-dev): bump @swc/core from 1.3.21 to 1.3.24 79ff209

1.2.3

16 December 2022

  • chore(deps-dev): bump @typescript-eslint/eslint-plugin 1abadf5
  • chore(deps-dev): bump @typescript-eslint/eslint-plugin 01480d1
  • chore(deps-dev): bump release-it from 15.5.0 to 15.5.1 397dd9f

1.2.2

28 November 2022

  • chore(deps-dev): bump @swc/helpers from 0.4.12 to 0.4.14 #8
  • chore(deps-dev): bump @types/jest from 29.2.2 to 29.2.3 #7
  • chore(deps-dev): bump @swc/core from 1.3.16 to 1.3.19 #6
  • chore(deps-dev): bump @typescript-eslint/eslint-plugin from 5.42.1 to 5.43.0 #5
  • chore(deps-dev): bump @typescript-eslint/parser from 5.42.1 to 5.43.0 #4
  • chore: upgrade dependencies cca09b1
  • chore: release v1.2.2 28420d9
  • chore(deps-dev): bump lint-staged from 13.0.3 to 13.0.4 bd30e70

1.2.1

16 November 2022

  • chore: release v1.2.1 7a12be9
  • chore: exclude dist/utils from npm package 2d2e24a

1.2.0

16 November 2022

  • feat: minify bundle and output to single file 1519a6b
  • chore: release v1.2.0 3768f7c

1.1.0

16 November 2022

  • feat: support boolean values 29c32c9
  • refactor: make PropertyPath type simpler ebdb01b
  • chore: release v1.1.0 f55c7cf

1.0.5

15 November 2022

  • chore: remove unused dependencies 43bfe3e
  • fix: build after removed too many dev dependencies ca67dae
  • chore: release v1.0.5 be72859

1.0.4

14 November 2022

  • chore: use local github token to release c4fffb0
  • chore: release v1.0.4 eaa589e
  • feat: export default sortByProperty 5600b75

1.0.3

14 November 2022

  • chore: release v1.0.3 ed07b37
  • fix: now really fixing module exports 11e5acc
  • chore(deps-dev): bump @swc/core from 1.3.14 to 1.3.16 93ebec1

1.0.2

14 November 2022

1.0.1

14 November 2022

  • feat: use rollup to create dist files c00fca7
  • chore: release v1.0.1 7877424

1.0.0

14 November 2022

  • test: better structure tests 4542c27
  • chore: update readme on performance da3d3e6
  • test: use toStrictEqual to also allow type checking undefined values 82a1401

0.6.0

11 November 2022

  • feat: added support for sorting symbols ded98a9
  • chore: release v0.6.0 4f7ac07
  • refactor: use method to get property values from names b91469c

0.5.1

11 November 2022

  • chore: fix ci on performance.ts abb49db
  • chore: release v0.5.1 39739c2

0.5.0

11 November 2022

  • feat: performance improvement on large arrays by not using reduce when the propertyPath is only 1 level b157a82
  • test: improve tests 413991b
  • chore: release v0.5.0 d7b78c3

0.4.4

11 November 2022

  • chore: update issue templates 4731c97
  • refactor: move supported types in SupportedTypes type e111016
  • refactor: do not split the path each time we sort an object d4705c5

0.4.3

8 November 2022

  • chore: release v0.4.3 f814891
  • feat: add dist folder to npm package 6caa045

0.4.2

8 November 2022

  • refactor: move bigint check down its probably the least used c579f8a
  • chore: release v0.4.2 2a37e62
  • test: add extra bigint test 2c2e4f3

0.4.1

8 November 2022

0.4.0

8 November 2022

  • feat: handle bigint values correctly b3ffaf3
  • chore: release v0.4.0 ed70cd8

0.3.0

8 November 2022

  • feat: handle null and undefined values correctly without erroring 1e8c726
  • chore: update packages and scripts 48ba614
  • feat: correctly sort strings disguised as a number 9438046

0.2.4

8 November 2022

  • chore: remove conventional changelog a8015da
  • chore: release v0.2.4 f9449e5

0.2.3

8 November 2022

0.2.2

8 November 2022

  • chore: create changelog on release 9c7b570
  • chore: use auto-changelog 96f0aaa
  • refactor: use array util for isArray 4f7cdbb

0.2.1

8 November 2022

  • docs: update readme with npm package name c408483
  • chore: release v0.2.1 0f72049
  • chore: create changelog on release 83c687f

0.2.0

8 November 2022