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

d3-comparator

v0.1.0

Published

Multi-dimensional comparator function generator for D3

Downloads

8

Readme

d3.comparator

A D3 generator for comparator functions. It can be used to easily sort arrays of objects by one or multiple dimensions.

If you want to sort by a single dimension, you end up writing comparators like function cmp(a, b) { return a.value - b.value; } or function cmp(a, b) { return d3.ascending(a.value, b.value); }. For sorting by two or more dimensions it gets ugly pretty fast. d3.comparator provides a simple API for generating those comparators.

API

d3.comparator()

Constructs a new comparator with the default return value 0. I.e. using it will not change sort order.

comparator(a, b)

The comparator can be used with the array sort method.

comparator.order(cmp, [accessor])

Adds a dimension to the comparator. The return value of accessor will be compared with cmp (which has to be a comparator function itself, e.g. d3.ascending). If accessor isn't specified, an identity function function(d) { return d; } is used.

Dimensions are compared in order. As soon as cmp returns something other than 0, subsequent dimensions are ignored.

Example using a single dimension:

var cmp = d3.comparator().order(d3.ascending, function(d) { return d.value; });

Example using two dimensions, roughly equivalent to SQL ORDER BY year DESC, value ASC:

var cmp = d3.comparator()
  .order(d3.descending, function(d) { return d.year; })
  .order(d3.ascending, function(d) { return d.value; });

Note: These are just comparator functions. To acually sort an array use

someArray.sort(cmp);

Author

Jeremy Stucki, Interactive Things

License

BSD, see LICENSE.txt