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

multisort

v0.5.3

Published

Sort an array using any number of separate, ranked criteria

Downloads

7,304

Readme

Multitest

A small library for sorting arrays by multiple criteria.

Installation

npm install multisort --save

bower install multisort --save

Basic Usage

var input = [
  {firstName: "Kate", lastName: "Bush"},
  {firstName: "George", lastName: "Bush", suffix: "Junior"},
  {firstName: "George", lastName: "Orwell"},
  {firstName: "George", lastName: "Bush", suffix: "Senior"},
];

var criteria = [
  'firstName',
  '~lastName.length',
  'suffix.charAt(1)'
];

multisort(inputArray, criteria)

// input is now sorted by firstName (ascending), then lastName.length (descending),
// and finally suffix.charAt, called with 1 as the argument:
// [
//  {firstName: "George", lastName: "Orwell"},
//  {firstName: "George", lastName: "Bush", suffix: "Senior"},
//  {firstName: "George", lastName: "Bush", suffix: "Junior"},
//  {firstName: "Kate", lastName: "Bush"},
// ];
var input = [8, 7, 6, 5, 4, 3, 2, 1];

var criteria = [
  function(a) {return a % 2},
  function(a) {return a % 3},
  function(a) {return a}
];

multisort(input, criteria);

// input is now:
// [6, 4, 2, 8, 3, 1, 7, 5]

Partial Application

// Passing a single argument makes a sorting function that can then be applied to lists.
var sortByMod2AndMod3 = multisort([
  function(a) {return a % 2},
  function(a) {return a % 3},
  function(a) {return a}
]);

var input1 = [8, 7, 6, 5, 4, 3, 2, 1];
sortByMod2AndMod3(input1);
// input1 is [6, 4, 2, 8, 3, 1, 7, 5]

var input2 = [1, 5, 10, 25, 50, 100];
sortByMod2AndMod3(input2);
// input2 is [10, 100, 50, 1, 25, 5]

Features

Criteria types:

  • Function: Applies the given function to each element, and the return values are sorted in ascending order, using < and >.
  • String: Picks out the property indicated by the string, and sorts in ascending order, using < and >. Nested properties can be accessed with dot syntax, as in "property.subproperty.subsubproperty"
  • !String or ~String: As above, but sorted in descending order. This works with the functional and existential operators below.
  • String(arg1, arg2...): As above, but calls the indicated property as a function with the given arguments. Arguments are split by commas, trimmed, and parsed with JSON.parse.
  • String?: As above, tests whether the property is null or undefined. Items without the property are sorted before items with it.

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 0.1.0 Initial release