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

deep-sort

v1.0.4

Published

Sort arrays of objects or dictionaries of objects by property (including nested properties).

Downloads

16

Readme

Deep-Sort

A handy utility function to sort arrays of objects or dictionaries of objects by property (including nested properties).

Quick Start

Sorting behaviour is the same as Array.sort(). See the /examples directory for some working examples.

Sorting an Array of Objects

You can sort an array of objects based on one of its (nested) properties. Arrays are sorted in place (the original array is mutated) and the result is returned.

const deepSort = require(`deep-sort`);

const myArray = [
	{ id: 1, nested: { time: 111, deeper: { text: `AAA` } } },
	{ id: 2, nested: { time: 222, deeper: { text: `BBB` } } },
	{ id: 3, nested: { time: 333, deeper: { text: `CCC` } } },
];

// Shorthand.
deepSort.array(myArray, `id`);  // Default is order ASC.
deepSort.array(myArray, `id`, `asc`);
deepSort.array(myArray, `id`, `desc`);
deepSort.array(myArray, `nested.time`, `asc`);
deepSort.array(myArray, `nested.deeper.text`, `desc`);
deepSort.array(myArray, `nested.deeper.text`, `desc`, comparatorFunction);

// Longhand (all arguments required).
deepSort(myArray, null, `id`, `asc`);
deepSort(myArray, null, `id`, `asc`, comparatorFunction);

Sorting a Dictionary of Objects

You can sort a dictionary of objects based on one of its (nested) properties. Dictionaries are not mutated, and a new dictionary will be returned.

const deepSort = require(`deep-sort`);

const myDictionary = {
	'key_1nd': { key: `key_1nd`, quantity: 98, nested: { time: 111, deeper: { text: `AAA` } } },
	'key_k8a': { key: `key_k8a`, quantity: 45, nested: { time: 222, deeper: { text: `BBB` } } },
	'key_aj3': { key: `key_aj3`, quantity: 1, nested: { time: 333, deeper: { text: `CCC` } } },
};

// Shorthand.
deepSort.object(myDictionary, `key`, `quantity`);  // Default is order ASC.
deepSort.object(myDictionary, `key`, `quantity`, `asc`);
deepSort.object(myDictionary, `key`, `quantity`, `desc`);
deepSort.object(myDictionary, `key`, `nested.time`, `asc`);
deepSort.object(myDictionary, `key`, `nested.deeper.text`, `desc`);
deepSort.object(myDictionary, `key`, `nested.deeper.text`, `desc`, comparatorFunction);

// Longhand (all arguments required).
deepSort(myDictionary, `key`, `quantity`, `quantity`, `asc`);
deepSort(myDictionary, `key`, `quantity`, `quantity`, `asc`, comparatorFunction);

Custom Comparator Function

If you need more control you can pass a comparator function to the deepSort.custom() method. You must return a numerical value from this function which can be accepted by Array.sort():

  • -1 - Sort itemA lower than itemB. (ASC).
  • 0 - Leave in place.
  • +1 - Sort itemA higher than itemB (DESC).
// Arrays.
deepSort.custom(array, resources => {

	// resources.itemA    -> the next item in the iterable.
	// resources.itemB    -> the next + 1 item in the iterable.
	// resources.iterable -> the input iterable (array).

	return an integer;

});

// Objects.
deepSort.custom(dictionary, `someKeyProperty`, resources => {

	// resources.itemA    -> the next item in the iterable.
	// resources.itemB    -> the next + 1 item in the iterable.
	// resources.iterable -> the input iterable (dictionary).

	return an integer;

});

If you pass a comparator function to deepSort(), deepSort.array() or deepSort.object() you get access to all the input arguments:

function comparator (resources) {

	// resources.propA         -> the next item property in the iterable (specified by sortProperty).
	// resources.propB         -> the next + 1 item property in the iterable (specified by sortProperty).
	// resources.itemA         -> the next item in the iterable.
	// resources.itemB         -> the next + 1 item in the iterable.
	// resources.iterable      -> the input iterable (array or dictionary).
	// resources.keyProperty   -> the input keyProperty (array or dictionary).
	// resources.sortProperty  -> the input sortProperty (array or dictionary).
	// resources.sortDirection -> the input sortDirection (array or dictionary).

	return an integer;

}

API Overview

deepSort(iterable, keyProperty, sortProperty, sortDirection = 'asc', comparator = null)

Sort the given iterable (array or dictionary) with all arguments available.

deepSort.array(array, sortProperty, sortDirection = 'asc', comparator = null)

Sort the given array using the given object path, ordering flag, and optional comparator.

The optional comparator function will be passed the following values (e.g. comparator (resources) { ... }):

  • resources.propA
  • resources.propB
  • resources.itemA
  • resources.itemB
  • resources.iterable
  • resources.keyProperty
  • resources.sortProperty
  • resources.sortDirection

deepSort.object(object, keyProperty, sortProperty, sortDirection = 'asc', comparator = null)

Sort the given object using the given key property, object path and custom comparator function.

The optional comparator function will be passed the following values (e.g. comparator (resources) { ... }):

  • resources.propA
  • resources.propB
  • resources.itemA
  • resources.itemB
  • resources.iterable
  • resources.keyProperty
  • resources.sortProperty
  • resources.sortDirection

deepSort.custom(iterable, comparator)

Sort the given iterable using the result of a custom comparator function.

The required comparator function will be passed the following values (e.g. comparator (resources) { ... }):

  • resources.itemA
  • resources.itemB
  • resources.iterable