summaryrefslogtreecommitdiff
path: root/lib/sortBy.js
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2015-12-23 15:50:39 -0500
committerGraeme Yeates <yeatesgraeme@gmail.com>2015-12-29 16:48:48 -0500
commit18e61d4f07f48604601f2effdbe2a7e188d14d4a (patch)
treed4761428f57ec80816bea109124fd836cdaf20a6 /lib/sortBy.js
parent7127b67f94a22247c36bf40e4f2685912e0f80e9 (diff)
downloadasync-18e61d4f07f48604601f2effdbe2a7e188d14d4a.tar.gz
[WIP] modularization (#984)
Diffstat (limited to 'lib/sortBy.js')
-rw-r--r--lib/sortBy.js23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/sortBy.js b/lib/sortBy.js
new file mode 100644
index 0000000..033a6c8
--- /dev/null
+++ b/lib/sortBy.js
@@ -0,0 +1,23 @@
+'use strict';
+
+import arrayMap from 'lodash/internal/arrayMap';
+import property from 'lodash/utility/property';
+
+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;
+ }
+}