Comparing version 1.2.2 to 1.3.0
12
index.js
@@ -17,2 +17,3 @@ 'use strict'; | ||
var y; | ||
var desc; | ||
@@ -23,2 +24,4 @@ if (typeof el === 'function') { | ||
} else if (typeof el === 'string') { | ||
desc = el.charAt(0) === '-'; | ||
el = desc ? el.slice(1) : el; | ||
x = dotPropGet(a, el); | ||
@@ -37,7 +40,12 @@ y = dotPropGet(b, el); | ||
if (typeof x === 'string' && typeof y === 'string') { | ||
ret = x.localeCompare(y); | ||
ret = desc ? y.localeCompare(x) : x.localeCompare(y); | ||
return ret !== 0; | ||
} | ||
ret = x < y ? -1 : 1; | ||
if (desc) { | ||
ret = x < y ? 1 : -1; | ||
} else { | ||
ret = x < y ? -1 : 1; | ||
} | ||
return true; | ||
@@ -44,0 +52,0 @@ }); |
{ | ||
"name": "sort-on", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Sort an array on an object property", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "ava" | ||
}, | ||
@@ -41,4 +41,4 @@ "files": [ | ||
"devDependencies": { | ||
"ava": "0.0.4" | ||
"ava": "*" | ||
} | ||
} |
@@ -16,3 +16,3 @@ # sort-on [![Build Status](https://travis-ci.org/sindresorhus/sort-on.svg?branch=master)](https://travis-ci.org/sindresorhus/sort-on) | ||
```js | ||
var sortOn = require('sort-on'); | ||
const sortOn = require('sort-on'); | ||
@@ -23,2 +23,6 @@ // sort by an object property | ||
// sort descending by an object property | ||
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], '-x'); | ||
//=> [{x: 'c'}, {x: 'b'}, {x: 'a'}] | ||
// sort by a nested object property | ||
@@ -28,2 +32,6 @@ sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], 'x.y'); | ||
// sort descending by a nested object property | ||
sortOn([{x: {y: 'b'}}, {x: {y: 'a'}}], '-x.y'); | ||
//=> [{x: {y: 'b'}, {x: {y: 'a'}}}] | ||
// sort by the `x` propery, then `y` | ||
@@ -34,5 +42,3 @@ sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['x', 'y']); | ||
// sort by the returned value | ||
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], function (el) { | ||
return el.x; | ||
}); | ||
sortOn([{x: 'b'}, {x: 'a'}, {x: 'c'}], el => el.x); | ||
//=> [{x: 'a'}, {x: 'b'}, {x: 'c'}] | ||
@@ -44,17 +50,15 @@ ``` | ||
### sortOn(array, property) | ||
### sortOn(input, property) | ||
Returns a new sorted array. | ||
#### array | ||
#### input | ||
*Required* | ||
Type: `array` | ||
Type: `Array` | ||
#### property | ||
*Required* | ||
Type: `string`, `function` or `array` of either | ||
Type: `String` `Array<String>` `Function` | ||
The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. | ||
The string can be a [dot path](https://github.com/sindresorhus/dot-prop) to a nested object property. Prepend it with `-` to sort it in descending order. | ||
@@ -64,2 +68,2 @@ | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
4180
44
64