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

sorty

v1.2.2

Published

Utility for sorting object arrays on multiple properties

Downloads

12,678

Readme

sorty

Sort object arrays on multiple properties with ease.

Install

$ npm install --save sorty

Note

sorty sorts the array in-place, as it uses Array.sort! See Array.sort on MDN

Usage

Example 1

var sorty = require('sorty')

var arr = [
    {name: 'john', age: 20},
    {name: 'mary', age: 10},
    {name: 'bill', age: 40},
    {name: 'john', age: 100}
]

sorty([
    {name: 'name', dir: 'asc'},
    {name: 'age',  dir: 'desc', type: 'number'}
], arr)


//arr.should.eql(
[
    {name: 'bill', age: 40},
    {name: 'john', age: 100},
    {name: 'john', age: 20},
    {name: 'mary', age: 10}
]
//)

Example 2

var sort = sorty([
    {name: 'name', dir: 'asc'},
    {name: 'age',  dir: 'desc', type: 'number'}
])

//now sort is a function that can be passed an array to sort it

sort(arr)

sorty returns the sorted array. Under the hoods, all it does is build a composed sort function, based on the given sort info, and call array.sort with that function.

You can specify a sort function in the sortInfo. The sort function should always sort in ascending order!. Actual sorting direction should be specified in the dir property.

Sort info

Example of valid sort info:

//an array
[
    //specify the type of values  - valid types: 'string' and 'number'
    { name: 'age', type: 'number'},
    { name: 'name', dir: 1 } //1 or asc (vs -1 or desc)
]

//an object - sort by only 1 property
{
    name: 'age'
}

//specify custom sort fn
[
    //since age may be string, but with numeric values, use a custom sort fn
    {name: 'age', fn: function(a, b){ return a * 1 - b * 1}, dir: 'desc' },
    {name: 'name', dir: 'asc'}
]

Valid sort types for now are:

  • 'string'
  • 'number'

The sort direction is specified in the dir property. Valid values are:

  • 1 (or 'asc', or any negative number)
  • -1 (or 'desc', or any positive number)

If you specify 0 (or any valsy value) for the sort direction, the sorting will not be done for the given property, but only for all other properties.

You can specify custom sort functions in the fn property. Those should always sort in ascending order! If there is a name on the sort info, sort function will recive values from those keys or it will receive the objects from the data array.

API

sorty(sortInfo, array) // => sorted array - sorty sorts the array in-place and returns it sorty(sortInfo) // => fn - returns a curried version of sorty, which can be passed in an array, and will sort it, based on the sortInfo that was specified sorty.getFunction(sortInfo) // => sorting function - sorty.getFunction returns the composed sort function, that can be used to sort an array.

More examples

You can get a curried version, and just pass in an array afterwards, to get back the sorted array

var sorty = require('sorty')
var sort  = sorty([
    {name: 'age', fn: function(a, b){ return a*1 - b * 1}, dir: 'desc' },
    {name: 'name', dir: 'asc'}
])

sort(arr)

You can get a sort function, and use it with array.sort

var sorty = require('sorty')
var sortFn  = sorty.getFunction([
    {name: 'age', fn: function(a, b){ return a*1 - b * 1}, dir: 'desc' },
    {name: 'name', dir: 'asc'}
])

arr.sort(sortFn)

Regular usage

var sorty = require('sorty')

var arr = [
    { age: '5', name: 'mary'},
    { age: '5', name: 'bob'},
    { age: '15', name: 'monica'},
    { age: '15', name: 'adam'}
]

sorty([
    {name: 'age', fn: function(a, b){ return a*1 - b * 1}, dir: 'desc' },
    {name: 'name', dir: 'asc'}
], arr)

arr.should.eql([
    { age: '15', name: 'adam'},
    { age: '15', name: 'monica'},
    { age: '5', name: 'bob'},
    { age: '5', name: 'mary'}
])
var sorty = require('sorty')

var arr = [
    { age: '5', name: 'mary'},
    { age: '5', name: 'bob'},
    { age: '15', name: 'monica'},
    { age: '15', name: 'adam'}
]

sorty(
    {name: 'name', dir: 'asc'}
, arr)

arr.should.eql([
    { age: '15', name: 'adam'},
    { age: '5', name: 'bob'},
    { age: '5', name: 'mary'},
    { age: '15', name: 'monica'}
])

Specifying 0 (or any falsy value) as sort dir will skip the sort for the given property

var sorty = require('sorty')

var arr = [
    { age: '5', name: 'mary'},
    { age: '5', name: 'bob'},
    { age: '15', name: 'monica'},
    { age: '15', name: 'adam'}
]

sorty([
    {name: 'age', dir: 0 },
    {name: 'name', dir: 1}
], arr)
//will sort only by name, asc

arr.should.eql([
    { age: '15', name: 'adam'},
    { age: '5', name: 'bob'},
    { age: '5', name: 'mary'},
    { age: '15', name: 'monica'}
])