diff options
Diffstat (limited to 'lib/sortBy.js')
-rw-r--r-- | lib/sortBy.js | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/sortBy.js b/lib/sortBy.js new file mode 100644 index 0000000..4f14823 --- /dev/null +++ b/lib/sortBy.js @@ -0,0 +1,23 @@ +'use strict'; + +import arrayMap from 'lodash/_arrayMap'; +import property from 'lodash/_baseProperty'; + +import map from './map'; + +export default function sortBy (arr, iterator, cb) { + map(arr, function (x, cb) { + iterator(x, function (err, criteria) { + if (err) return cb(err); + cb(null, {value: x, criteria: criteria}); + }); + }, function (err, results) { + if (err) return cb(err); + cb(null, arrayMap(results.sort(comparator), property('value'))); + }); + + function comparator(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + } +} |