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 🙏

© 2026 – Pkg Stats / Ryan Hefner

compare-by

v2.1.1

Published

A versatile utility library for sorting arrays of objects by one or multiple keys with customizable sort directions.

Readme

compare-by

TypeScript npm Buy Me a Coffee Tests Build Release

Type-safe comparators for sorting arrays by one or more object properties, including nested values selected by a callback.

Installation

pnpm add compare-by
# or
npm install compare-by

Usage

import { compareBy } from 'compare-by';

const people = [
	{ name: { first: 'Ada', last: 'Lovelace' }, age: 36, active: true },
	{ name: { first: 'Grace', last: 'Hopper' }, age: 85, active: false },
];

// One property; `asc` is the default direction.
people.sort(compareBy({ key: 'age' }));

// A callback can select a nested property.
people.sort(compareBy({ key: (person) => person.name.last, dir: 'desc' }));

// Compare properties in order until one differs.
people.sort(compareBy([
	{ key: 'active', dir: 'desc' },
	{ key: (person) => person.name.last },
]));

API

compareBy(props)

Returns an Array.prototype.sort comparator. props accepts one CompareKey or an array of them. Keys are evaluated in order, so later keys break ties from earlier keys.

type CompareDirection = 'asc' | 'desc';
type CompareKey<T> = {
	key: keyof T | ((value: T) => unknown);
	dir?: CompareDirection;
};

function compareBy<T>(props: CompareKey<T> | CompareKey<T>[]): (a: T, b: T) => number;

The selected values must be strings, numbers, booleans, or Date instances. Other values cause compareBy to throw an Unsupported data type for comparison error when sorting.

Value comparators

The package also exports compareStrings, compareNumbers, compareBooleans, compareDates, and compareValues for direct use. All accept (a, b, dir?) and return a negative number, zero, or a positive number.

  • Strings use localeCompare.
  • Dates compare their timestamps.
  • Booleans sort true before false in ascending order.
  • compareValues dispatches to the matching comparator and throws for unsupported or mixed value types.

Development

This repository uses pnpm 11.

pnpm install --frozen-lockfile
pnpm lint
pnpm test
pnpm build

out/ is generated during the build and before npm publication; do not commit it. Add a changeset with pnpm changeset for every user-facing change. The release workflow creates a version PR and publishes it after that PR is merged.

Contributing

Please use the bug report or feature request template. Include a focused test with behavior changes.

License

MIT

Author

Tobias Wälde