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

chainsorter

v0.2.0

Published

Sort a list based on properties of its elements or by any arbitrary functions you define. Chain them together if you like.

Downloads

8

Readme

npm

ChainSorter

Sort your data in a single line – or more, if you like.

I got tired of writing sort() compare functions, so I made this general purpose sorting helper, which I'm now spinning off into a little library of its own. Maybe it'll help someone else, only time and download stats will tell.

ChainSorter uses sort() under the hood, but allows you to simply define how you want that sort to work. It does so by exposing the functions asc() and desc(), which you can pass multiple parameters to, and, of course, can chain.

The actual sort won't take place until you call sort(), which will return the data for immediate use.

Check out the demo to get a feel for if ChainSorter is right for you: altef.github.io/chainsorter.

Getting started

Download the script or, npm install chainsorter.

Include it with whichever method is appropriate:

<script src="chainsorter.js"></script>
const {ChainSorter} = require('chainsorter');
import {ChainSorter} from 'chainsorter';

Now, say you have an array of data such as:

const names = [
    {
		"first": "John",
		"last": "Doe"
	},
	{
		"first": "Jane",
		"last": "Smith"
	},
	{
		"first": "John",
		"last": "Rabble"
	},
	{
		"first": "Jack",
		"last": "Doe"
	}
];

You can define your sort using the asc() and desc() functions. Each take one or more params and can be chained. asc('first', 'last') is equivalent to asc('first').asc('last').

const byFirstNameZtoA = ChainSorter(names).desc('first').sort();

// By last name, and if any last names are equivalent by first name
const Atoz = ChainSorter(names).asc('last', 'first').sort();

// The same as the last one, but using an expanded array
const same = ChainSorter(names).asc(...['first', 'last']).sort();

// Or you can just pass in the array
const orUseAnArray = ChainSorter(names).asc(['first', 'last']).sort();

The result will be sorted in priority of your definition from left to right. Basically whichever field you pass to ChainSorter first will have the highest sort priority. It will continue on to the next field/rule should that resolve a comparison as equal, and so on.

But what if you want to sort in some method that's more complicated than a niave comparison on object properties? Well good news! ChainSorter supports custom comparitor functions of its own. And of course you can pass in as many as you want and chain them to your heart's content. It's easily done through the custom() function.

Functions

ChainSorter(data)

Returns a new ChainSorter object for your data.

asc(field)

Sort by a field (or fields) in ascending order. You can pass multiple fields to this. It returns the ChainSorter object for, you know, chaining. This is exactly the same as the desc() function, but ascending.

desc(field)

Sort by a field (or fields) in descending order. You can pass multiple fields to this. It returns the ChainSorter object for, again, chaining. This is exactly the same as the asc() function, but descending.

custom(fn)

Sort by a custom function. You can pass multiple functions to this if you like. It returns the ChainSorter object as well. Your compare function should be similar to one passed to javascript's sort() function – take two parameters and return -1, 0, or 1.

(a,b) => { 
    if (a > b) return -1;
    if (b > a) return 1;
    return 0;
}

sort()

Perform the sort using the order you've defined, and return the data for immediate use.