npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sort-by-property

v1.3.0

Published

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

Downloads

12,130

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.