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

@romanhavryliv/deep-sorting

v1.1.1

Published

Sorting function for arrays of objects.

Downloads

241

Readme

Deep Sorting

Sorting function for arrays of objects.

install

npm install @romanhavryliv/deep-sorting

usage

import deepSorting from '@romanhavryliv/deep-sorting';
...

deepSorting(arrayToSort, arrayOfSortingCriteria)
  • arrayToSort - an array that needs to be sorted (is sorted in place).
  • arrayOfSortingCriteria - an array of sorting criteria. Criteria can be a string, a function or an array of two elements where the first one is criteria itself (a string or a function) and the second one is descendic pointer. This "descendic pointer" points to sort the array by this particular criteria using descendic order. "Descendic" pointer should be only "desc" string. Any other value is ignored. The order of criteria in array is important for sorting order.

examples

For example we have a database of pupils as an array of objects pupils = [pupil1, pupil2, pupil3, ...]. And we have next structure of our objects (pupil1, pupil2, pupil3, ...):

{
  name: {
    firstName,
    lastName,
  },
  marks: {
    history, // e.g. [A, A, B, D, A],
    arts, // e.g. [A, A, A],
  },
}

String sorting criteria is a path in pupil object to the primitive that must be compared for sorting. String criteria can begin either with "." (period) or without it. '.name.firstName' and 'name.firstName' are equal.

Also it can start with "[" (opening square bracket)

`[${name}].firstName`

If we want to sort pupils by name we should write next '.name.firstName'. And our arrayOfSortingCriteria would look like next arrayOfSortingCriteria = ['.name.firstName']. Or with descendic order: arrayOfSortingCriteria = [['.name.firstName', 'desc']].

Function sorting criteria is a function that return some primitive that can be compared for sorting. If we want to sort pupils by number of marks in History we should create next function

const historyMarksNumber = (pupil) => pupil.marks.history.length;

or with descendic order:

const historyMarksNumber = (pupil) => [pupil.marks.history.length, 'desc'];

and arrayOfSortingCriteria = [historyMarksNumber].

What is the purpose of DEEP sorting?

According to examples above we can combine those sorting methods.

If we pass next arrayOfSortingCriteria = ['.name.firstName', historyMarksNumber] we can have such result as

| Name | History marks number | | ---- | -------------------- | | Adam | 8 | | Adam | 7 | | Adam | 5 | | Bob | 7 | | Eric | 8 |

Here we have three pupils with name Adam and they are sorted by "History marks number" as well.

Order of criteria in array is important for sorting order!

For replaced criteria arrayOfSortingCriteria = [historyMarksNumber, '.name.firstName'] result is

| Name | History marks number | | ---- | -------------------- | | Adam | 8 | | Eric | 8 | | Adam | 7 | | Bob | 7 | | Adam | 5 |

Now we have two pupils with "History marks number" of "8" and two of "7" and they are sorted by "firstName" as well (inside of mark "8" and "7").

Let's see the second table with descendic order for the second criteria arrayOfSortingCriteria = [historyMarksNumber, ['.name.firstName', 'desc']]:

| Name | History marks number | | ---- | -------------------- | | Eric | 8 | | Adam | 8 | | Bob | 7 | | Adam | 7 | | Adam | 5 |

Now we have descendic order of pupils by their firstName inside the list with the same "History marks number".

Object complexity

The object (pupil) can be of any level of complexity combining objects and arrays inside.

Few more examples for string criteria:

'.marks.arts[1]' - to sort pupils by "second mark of Arts".

`.marks[${subject}][1]` - to sort pupils by second mark of a "subject" passed as variable.

Function complexity depends only on your needs. The only thing it must do - return a primitive for sorting comparison.

arrayOfSortingCriteria can contain only strings, only functions or any combination of those.

If an empty array is passed as arrayOfSortingCriteria then regular sorting is applied (it doesn't change anything in array of objects).

Change log (to v 1.1.x)

  • fixed leading period issue
  • added descentic order of sorting
  • added typescript