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

sortilege

v1.0.4

Published

A function to sort (about) everything

Downloads

3

Readme

Sort arrays containing any data type by given field(s)!

NPM version Maintainability Open Source Love

Installation

npm: npm i -s sortilege

yarn: yarn add sortilege

unpkg: https://unpkg.com/sortilege

Usage

Sortilege is a function which accepts an array to sort and an optional options object and returns a sorted array.

const sorted = sortilege(array[, options])

The most basic example is about sorting a simple array of strings. It sorts in ascending order by default:

import sort from "sortilege";

const simpleArray = ["pineapple", "apple", "banana", "pear"];

return sort(simpleArray);
// ["apple","banana","pear","pinapple"];

If an array of objects is passed in, when no options are specified, sortilege uses the first non-object field it finds traversing the tree.

const users = [
  { user: { name: "luke", age: 32 } },
  { user: { name: "andrew", age: 40 } },
  { user: { name: "mary", age: 43 } },
  { user: { name: "andrew", age: 29 } }
];

return sort(users);
// the field [name] is used to sort
// [
// { user: { name: "andrew", age: 43 } },
// { user: { name: "andrew", age: 29 } },
// { user: { name: "luke", age: 32 } },
// { user: { name: "mary", age: 43 } },
// ];

If an array of arrays is passed in, sortilege uses the 0th element of the children arrays to sort by.

const users = [
  ["Mark", "Ross", 30, "Student"],
  ["Paul", "White", 29, "Student"],
  ["Jessica", "Bishop", 32, "Artist"]
];

return sort(users);
// the element with index 0 is used to sort by
// [
// ["Jessica", "Bishop", 32, "Artist"]
// ["Mark", "Ross", 30, "Student"],
// ["Paul", "White", 29, "Student"],
// ]

Options

The options object is optional and has the following structure:

const options = {
  sortDir: string, // specifies the direction of sort. Possible values are ASC (default) and DESC
  sortBy: string | array[string], // specifies the field(s) to sort by. See above for more details.
  throwError: bool // specifies whether or not an error has to be thrown in case of issues
};

sortDir

With sortDir you can specify the sort direction. It accepts both "ASC" and "DESC". By default it is set to "ASC".

const users = [
  { user: { name: "luke", age: 32 } },
  { user: { name: "andrew", age: 40 } },
  { user: { name: "mary", age: 43 } },
  { user: { name: "andrew", age: 29 } }
];

return sort(users, { sortDir: "DESC" });
// the field [name] is used to sort
// [
// { user: { name: "luke", age: 32 } },
// { user: { name: "mary", age: 43 } },
// { user: { name: "andrew", age: 40 } },
// { user: { name: "andrew", age: 29 } }
// ];

In the previous example our array is sorted, in descending order, by the first valuable field which is name.

sortBy

Specifies the field(s) the array is sorted by.

It must refer to non-object and non-array field. It can have the following formats.


Single field

It finds the field specified inside the object and sort by it.

sortBy: "field";

Example:

const users = [
  { id: 1, name: "luke", age: 32 },
  { id: 2, name: "andrew", age: 40 },
  { id: 3, name: "mary", age: 43 },
  { id: 4, name: "andrew", age: 29 }
];

return sort(users, { sortBy: "age", sortDir: "DESC" });
// [
// { id: 3, name: "mary", age: 43 },
// { id: 2, name: "andrew", age: 40 },
// { id: 1, name: "luke", age: 32 },
// { id: 4, name: "andrew", age: 29 },
// ]

It is possible to use . to access fields on nested objects. In this case just the last field in the path has to be a non-object and non-array type. It is recursive so there is no limit to the nesting levels.

sortBy: "field.subfield";

Example:

const users = [
  { id: 1, name: "luke", address: { city: { name: "Manchester", zipCode: "" } } },
  { id: 2, name: "andrew", address: { city: { name: "Berlin", zipCode: "" } } },
  { id: 3, name: "mary", address: { city: { name: "Paris", zipCode: "" } } },
  { id: 4, name: "andrew", address: { city: { name: "Bruxelles", zipCode: "" } } }
];

return sort(users, { sortBy: "address.city.name", sortDir: "DESC" });
// [
// { id: 3, name: "mary", address: { city:{ name: "Paris", zipCode: "" } } },
// { id: 1, name: "luke", address: { city:{ name: "Manchester", zipCode: "" } } },
// { id: 4, name: "andrew", address: { city:{ name: "Bruxelles", zipCode: "" } } },
// { id: 2, name: "andrew", address: { city:{ name: "Berlin", zipCode: "" } } },
// ]

In case of array of arrays, sortBy can specify the index of the element the array has to be sort by.

const users = [
  ["Mark", "Ross", 30, "Student"],
  ["Paul", "White", 29, "Student"],
  ["Jessica", "Bishop", 32, "Artist"]
];

return sort(users, { sortBy: "2", sortDir: "DESC" });
// [
// ["Jessica", "Bishop", 32, "Artist"],
// ["Mark", "Ross", 30, "Student"],
// ["Paul", "White", 29, "Student"],
// ]

Multiple fields

It is possible to sort by multiple fields. To accomplish this, you can pass to sortBy an array of fields. The order in the array is important: the leftmost field is the most important in the sort, the rightmost the less important.

sortBy: ["field1", "field2", "field3.subfield"];

Example:

const users = [
  { id: 1, name: "luke", address: { city: { name: "Paris", zipCode: "" } } },
  { id: 2, name: "andrew", address: { city: { name: "Bruxelles", zipCode: "" } } },
  { id: 3, name: "mary", address: { city: { name: "Manchester", zipCode: "" } } },
  { id: 4, name: "andrew", address: { city: { name: "Berlin", zipCode: "" } } }
];

return sort(users, { sortBy: ["name", "address.city.name"] });
// [
// { id: 4, name: "andrew", address: { city: { name: "Berlin", zipCode: "" } } }
// { id: 2, name: "andrew", address: { city: { name: "Bruxelles", zipCode: "" } } },
// { id: 1, name: "luke", address: { city: { name: "Paris", zipCode: "" } } },
// { id: 3, name: "mary", address: { city: { name: "Manchester", zipCode: "" } } },
// ]

throwError

If throwError is set to false (default), in case of error sortilege fails silently and returns the passed array as is. When throwError is set to true instead, an Error is thrown. Example:

const inconsistentUsers = [
  { id: 1, name: "luke", age: 32 },
  { id: 2, age: 40 },
  { id: 3, name: "mary", age: 43 },
  { id: 4, name: "andrew", age: 29 }
];

return sort(inconsistentUsers, {
  sortBy: "name",
  sortDir: "DESC",
  throwError: true
});
// Error: "Specified sortBy (name) has not been found on item { id: 2, age: 40 }"

Changelog

CHANGES.md

License

MIT © Dario Rinaldi & Emiliano Costanzo