Giter Club home page Giter Club logo

d3-comparator's Introduction

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.dimension(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().dimension(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()
  .dimension(d3.descending, function(d) { return d.year; })
  .dimension(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

d3-comparator's People

Contributors

grossbart avatar jstcki avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.