Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ruanyl/sort-it
https://github.com/ruanyl/sort-it
Last synced: 18 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ruanyl/sort-it
- Owner: ruanyl
- License: mit
- Created: 2015-09-15T20:57:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-20T11:29:30.000Z (over 8 years ago)
- Last Synced: 2024-12-05T17:44:14.634Z (about 1 month ago)
- Language: JavaScript
- Size: 3.91 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
sort-it
========
[![build status](https://secure.travis-ci.org/ruanyl/sort-it.svg)](http://travis-ci.org/ruanyl/sort-it)
[![NPM version](https://badge.fury.io/js/sort-it.svg)](http://badge.fury.io/js/sort-it)sort
## Installation
This module is installed via npm:
``` bash
$ npm install sort-it
```## Example Usage
``` js
var sortIt = require('sort-it');var arr = [
{firstName: 'abc', lastName: 'efg', address: {country: 'FR'}},
{firstName: 'bcd', lastName: 'fgh', address: {country: 'CA'}},
{firstName: 'bcd', lastName: 'egh', address: {country: 'US'}},
{firstName: 'cde', lastName: 'ghi', address: {country: 'CA'}},
{firstName: 'def', lastName: 'hij', address: {country: 'BE'}}
];test(function (t) {
var result = sortIt(arr, 'lastName');
t.assert(result[0].lastName === 'efg');
t.assert(result[4].lastName === 'hij');
});test(function (t) {
var result = sortIt(arr, '-firstName');
t.assert(result[0].firstName === 'def');
t.assert(result[4].firstName === 'abc');
});test(function (t) {
var result = sortIt(arr, ['firstName', 'lastName']);
t.assert(result[0].firstName === 'abc');
t.assert(result[4].firstName === 'def');
t.assert(result[1].lastName === 'egh');
t.assert(result[2].lastName === 'fgh');
});test(function (t) {
var result = sortIt(arr, ['firstName', '-lastName']);
t.assert(result[0].firstName === 'abc');
t.assert(result[4].firstName === 'def');
t.assert(result[1].lastName === 'fgh');
t.assert(result[2].lastName === 'egh');
});test(function (t) {
var result = sortIt(arr, ['firstName', '-lastName']);
t.assert(result[0].firstName === 'abc');
t.assert(result[4].firstName === 'def');
t.assert(result[1].lastName === 'fgh');
t.assert(result[2].lastName === 'egh');
});test('asc by address.country', function (t) {
var result = sortIt(arr, 'address.country');
t.assert(result[0].address.country === 'BE');
t.assert(result[1].address.country === 'CA');
t.assert(result[2].address.country === 'CA');
t.assert(result[3].address.country === 'FR');
t.assert(result[4].address.country === 'US');
});
```