summaryrefslogtreecommitdiff
path: root/build-es/sortBy.js
blob: 75d61ca3cdb22f984a67c40a812160438284d641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'use strict';

import arrayMap from 'lodash-es/_arrayMap';
import property from 'lodash-es/_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;
    }
}