forked from ecrmnn/collect.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
var _require = require('../helpers/is'), | ||
isFunction = _require.isFunction; | ||
var nestedValue = require('../helpers/nestedValue'); | ||
var getValue = function getValue(item, valueOrFunction) { | ||
if (isFunction(valueOrFunction)) { | ||
return valueOrFunction(item); | ||
} | ||
return nestedValue(item, valueOrFunction); | ||
}; | ||
module.exports = function sortByMany() { | ||
var valuesOrFunctions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; | ||
var collection = [].concat(this.items); | ||
collection.sort(function (a, b) { | ||
for (var index = 0; index < valuesOrFunctions.length; index += 1) { | ||
var criteria = valuesOrFunctions[index]; | ||
var valueA = getValue(a, criteria); | ||
var valueB = getValue(b, criteria); | ||
if (valueA === null || valueA === undefined) { | ||
return 1; | ||
} | ||
if (valueB === null || valueB === undefined) { | ||
return -1; | ||
} | ||
if (valueA < valueB) { | ||
return -1; | ||
} | ||
if (valueA > valueB) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
}); | ||
return new this.constructor(collection); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# `sortByMany()` | ||
|
||
The sortByMany method sorts the collection by the given key. The sorted collection keeps the original array keys, so in this example we'll use the values method to reset the keys to consecutively numbered indexes: | ||
|
||
```js | ||
const collection = collect([ | ||
{ name: 'Desk', price: 200 }, | ||
{ name: 'Chair', price: 100 }, | ||
{ name: 'Bookcase', price: 150 }, | ||
]); | ||
|
||
const sorted = collection.sortByMany(['price']); | ||
|
||
sorted.all(); | ||
|
||
// [ | ||
// { name: 'Chair', price: 100 }, | ||
// { name: 'Bookcase', price: 150 }, | ||
// { name: 'Desk', price: 200 }, | ||
// ] | ||
``` | ||
|
||
You can use multiple keys to sort. Keys are considered in the order they appear in the parameter. | ||
|
||
```js | ||
const collection = collect([ | ||
{ name: 'Desk', price: 200 }, | ||
{ name: 'Table', price: 200 }, | ||
{ name: 'Chair', price: 100 }, | ||
{ name: 'Bookcase', price: 150 }, | ||
]); | ||
|
||
const sorted = collection.sortByMany(['price', 'name']); | ||
|
||
sorted.all(); | ||
|
||
// [ | ||
// { name: 'Chair', price: 100 }, | ||
// { name: 'Bookcase', price: 150 }, | ||
// { name: 'Desk', price: 200 }, | ||
// { name: 'Table', price: 200 }, | ||
// ] | ||
``` | ||
|
||
You can use dot notation to sort by nested values | ||
```js | ||
const collection = collect([ | ||
{ | ||
name: 'Desk', | ||
price: 200, | ||
manufacturer: { | ||
name: 'IKEA', | ||
}, | ||
}, | ||
{ | ||
name: 'Chair', | ||
price: 100, | ||
manufacturer: { | ||
name: 'Herman Miller', | ||
}, | ||
}, | ||
{ | ||
name: 'Bookcase', | ||
price: 150, | ||
manufacturer: { | ||
name: 'IKEA', | ||
}, | ||
}, | ||
]); | ||
|
||
const sorted = collection.sortByMany(['manufacturer.name']); | ||
|
||
sorted.all(); | ||
|
||
// [ | ||
// { | ||
// name: 'Chair', | ||
// price: 100, | ||
// manufacturer: { | ||
// name: 'Herman Miller', | ||
// }, | ||
// }, | ||
// { | ||
// name: 'Desk', | ||
// price: 200, | ||
// manufacturer: { | ||
// name: 'IKEA', | ||
// }, | ||
// }, | ||
// { | ||
// name: 'Bookcase', | ||
// price: 150, | ||
// manufacturer: { | ||
// name: 'IKEA', | ||
// }, | ||
// }, | ||
// ] | ||
``` | ||
|
||
You can also pass your own callback to determine how to sort the collection values: | ||
|
||
```js | ||
const collection = collect([ | ||
{ name: 'Desk', colors: ['Black', 'Mahogany'] }, | ||
{ name: 'Chair', colors: ['Black'] }, | ||
{ name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
|
||
const sorted = collection.sortByMany([(product, key) => product.colors.length]); | ||
|
||
sorted.all(); | ||
|
||
// [ | ||
// { name: 'Chair', colors: ['Black'] }, | ||
// { name: 'Desk', colors: ['Black', 'Mahogany'] }, | ||
// { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }, | ||
// ] | ||
``` | ||
|
||
You can also pass multiple callbacks of your own to determine how to sort the collection values: | ||
```js | ||
const collection = collect([ | ||
{ name: 'Desk', colors: ['Black', 'Mahogany'] }, | ||
{ name: 'Chair', colors: ['Black', 'Mahogany'] }, | ||
{ name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }, | ||
]); | ||
|
||
const sorted = collection.sortByMany([ | ||
(product, key) => product.colors.length, | ||
(product, key) => product.name, | ||
]); | ||
|
||
sorted.all(); | ||
|
||
// [ | ||
// { name: 'Chair', colors: ['Black', 'Mahogany'] }, | ||
// { name: 'Desk', colors: ['Black', 'Mahogany'] }, | ||
// { name: 'Bookcase', colors: ['Red', 'Beige', 'Brown'] }, | ||
// ] | ||
``` | ||
|
||
[View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/sortByMany.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.