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

gpii-sort

v1.0.0-dev.20170420T114825Z.804ac19

Published

A library to enable lucene-like sorting of array content.

Downloads

7

Readme

gpii-sort

A library that contains static functions to sort array content using lucene-like syntax. To ensure stable sorting, we use the fluid.stableSort function provided by Infusion.

gpii.sort(array, sortCriteria)

  • array: The array to be sorted.
  • sortCriteria: An array of sort terms. These are applied in reverse order (see examples below).
  • Returns: The sorted array.

Note that as with Array.sort, the order of the original array is modified.

Usage Examples

The syntax follows the same conventions as couchdb-lucene, namely, a sort option looks like a field name, and is optionally prefixed with a direction indicator (/ for ascending, \ for descending order). If there is no prefix, ascending order is assumed.

We also support type-specific sorting using an optional suffix enclosed in less than an greater than signs. For example, \updated<date> would sort records by the date when they were updated, in descending order.

As with couchdb-lucene we support sorting by 'float', 'double', 'int', 'long' and 'date'. If no suffix is specified, we default to "natural" sorting, i.e. whatever Javascript thinks is appropriate (normally, alphabetical sorting).

Let's say we have data like the following:

var myArray = [
    { color: "red", weight: 0.025, name: "Strawberry" },
    { color: "red", weight: 0.1, name: "Apple" },
    { color: "red", weight: 1500, name: "Sports Car" },
    { color: "blue", weight: 0.001, name: "Blueberry" },
    { color: "blue", weight: 0.9, name: "Crab" },
    { color: "blue", weight: 1300, name: "Family Sedan" }
];

A simple sort might look like:

gpii.sort(myArray, "color");

That would return the following:

[
    { color: "blue", weight: 0.001, name: "Blueberry" },
    { color: "blue", weight: 0.9, name: "Crab" },
    { color: "blue", weight: 1300, name: "Family Sedan" },
    { color: "red", weight: 0.025, name: "Strawberry" },
    { color: "red", weight: 0.1, name: "Apple" },
    { color: "red", weight: 1500, name: "Sports Car" }
]

Note that within each color, the items appear in the same order as in the original array. Here's a more complex example:

gpii.sort(myArray, ["color", "\weight"]);

The sort would result in the following order:

[
    { color: "blue", weight: 1300, name: "Family Sedan" },
    { color: "blue", weight: 0.9, name: "Crab" },
    { color: "blue", weight: 0.001, name: "Blueberry" },
    { color: "red", weight: 1500, name: "Sports Car" }
    { color: "red", weight: 0.1, name: "Apple" },
    { color: "red", weight: 0.025, name: "Strawberry" },
]

Note that as with Array.sort, comparisons between types are handled by comparing the string value of each object, as in the following example:

gpii.sort([{ a: false }, { a: 0 }, { a: "a string" }], "a");

// returns [{ a: 0 }, { a: "a string" }, { a: false }]