3 Object.defineProperty(exports, "__esModule", {
6 exports.default = sortBy;
8 var _arrayMap = require('lodash/_arrayMap');
10 var _arrayMap2 = _interopRequireDefault(_arrayMap);
12 var _baseProperty = require('lodash/_baseProperty');
14 var _baseProperty2 = _interopRequireDefault(_baseProperty);
16 var _map = require('./map');
18 var _map2 = _interopRequireDefault(_map);
20 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23 * Sorts a list by the results of running each `coll` value through an async
28 * @memberOf module:Collections
30 * @category Collection
31 * @param {Array|Iterable|Object} coll - A collection to iterate over.
32 * @param {Function} iteratee - A function to apply to each item in `coll`.
33 * The iteratee is passed a `callback(err, sortValue)` which must be called once
34 * it has completed with an error (which can be `null`) and a value to use as
35 * the sort criteria. Invoked with (item, callback).
36 * @param {Function} callback - A callback which is called after all the
37 * `iteratee` functions have finished, or an error occurs. Results is the items
38 * from the original `coll` sorted by the values returned by the `iteratee`
39 * calls. Invoked with (err, results).
42 * async.sortBy(['file1','file2','file3'], function(file, callback) {
43 * fs.stat(file, function(err, stats) {
44 * callback(err, stats.mtime);
46 * }, function(err, results) {
47 * // results is now the original array of files sorted by
51 * // By modifying the callback parameter the
52 * // sorting order can be influenced:
55 * async.sortBy([1,9,3,5], function(x, callback) {
57 * }, function(err,result) {
62 * async.sortBy([1,9,3,5], function(x, callback) {
63 * callback(null, x*-1); //<- x*-1 instead of x, turns the order around
64 * }, function(err,result) {
68 function sortBy(coll, iteratee, callback) {
69 (0, _map2.default)(coll, function (x, callback) {
70 iteratee(x, function (err, criteria) {
71 if (err) return callback(err);
72 callback(null, { value: x, criteria: criteria });
74 }, function (err, results) {
75 if (err) return callback(err);
76 callback(null, (0, _arrayMap2.default)(results.sort(comparator), (0, _baseProperty2.default)('value')));
79 function comparator(left, right) {
80 var a = left.criteria,
82 return a < b ? -1 : a > b ? 1 : 0;
85 module.exports = exports['default'];