• Stars
    star
    221
  • Rank 179,773 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A utility to create comparator functions for the native `Array.sort()`. Allows for sorting by multiple properties.

sort-by travis ci

A utility to create comparator functions for the native Array.sort() in both node and the browser. Allows for sorting by multiple properties.

Inspired by this StackOverflow answer.


Example

var sortBy = require('sort-by'),
    users = [];

users = [{
    id: 7,
    name: 'Foo',
    age: '34',
    email: { primary: '[email protected]' }
}, {
    id: 3,
    name: 'Baz',
    age: '67',
    email: { primary: '[email protected]' }
}, {
    id: 4,
    name: 'Bar',
    age: '67',
    email: { primary: '[email protected]' }
}];

users.sort(sortBy('name', 'age'));

/**
*   result:
*       [{id: 4, name: 'Bar', age: '67', email: { primary: '[email protected]' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: '[email protected]' }},
*       {id: 7, name: 'Foo', age: '34', email: { primary: '[email protected]' }}]
*/

/**
* Use `-` to reverse the sort order
*/

users.sort(sortBy('-id', 'name'));

/*
*   result:
*       [{id: 7, name: 'Foo', age: '34', email: { primary: '[email protected]' }},
*       {id: 4, name: 'Bar', age: '67', email: { primary: '[email protected]' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: '[email protected]' }}]
*/

/**
* Use `.` notation to traverse nested properties. See [object-path](https://www.npmjs.org/package/object-path) npm module for support.
*/

users.sort(sortBy('age', 'email.primary'));

/*
*   result:
*       [{id: 7, name: 'Foo', age: '34', email: { primary: '[email protected]' }},
*       {id: 4, name: 'Bar', age: '67', email: { primary: '[email protected]' }},
*       {id: 3, name: 'Baz', age: '67', email: { primary: '[email protected]' }}]
*/

Test in node

git clone https://github.com/staygrimm/sort-by.git
cd sort-by && make test-node

Test in the browser

git clone https://github.com/staygrimm/sort-by.git
cd sort-by && make test

License

(The MIT License)

Copyright (c) 2013 Kevin Neff [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

hackernews-choo

A Hacker News reader built with Choo
JavaScript
29
star
2

passwordless-rethinkdbstore

RethinkDB TokenStore for Passwordless
JavaScript
12
star
3

darkorlight

A tiny utility that checks whether a color is dark or light, for node.js or the browser.
JavaScript
7
star
4

deku-testutils

A collection of utilities for testing Deku components
JavaScript
5
star
5

township-media

A Township plugin that adds routes and methods for managing media uploads
JavaScript
5
star
6

localstorage-ns

Namespaced key/value store backed by localStorage with events and default value loading.
JavaScript
4
star
7

obj-subset

Determine if one object is a subset of another object
JavaScript
3
star
8

s3-public-url

Create a string representing a public AWS S3 file url.
JavaScript
3
star
9

modella-pouchdb

PouchDB adaptor for modella
JavaScript
3
star
10

ractive-adaptors-modella

Ractive adaptor for modella
JavaScript
2
star
11

ractive-adaptors-betterarray

Ractive adaptor for array
JavaScript
2
star
12

node-list

Returns an object list with all IDs or NAMEs within an element or document.
JavaScript
1
star
13

compat-trigger-event

JavaScript
1
star
14

duo-markdown

Require markdown as html strings for Duo
JavaScript
1
star
15

img-loaded

Run a callback function once an image has loaded
JavaScript
1
star
16

deku-component-find-class

Traverse a Deku component tree and return all components with a given class name.
JavaScript
1
star
17

deku-component-find-all

Traverse a component tree and return all components that satisfy a function.
JavaScript
1
star
18

deku-component-is-node

Determine if an object is a valid Deku node
JavaScript
1
star
19

deku-component-mount

Mount a Deku component to the DOM
JavaScript
1
star