No Description

sortBy.js 702B

1234567891011121314151617181920212223242526
  1. var _cb = require('./_cb.js');
  2. var pluck = require('./pluck.js');
  3. var map = require('./map.js');
  4. // Sort the object's values by a criterion produced by an iteratee.
  5. function sortBy(obj, iteratee, context) {
  6. var index = 0;
  7. iteratee = _cb(iteratee, context);
  8. return pluck(map(obj, function(value, key, list) {
  9. return {
  10. value: value,
  11. index: index++,
  12. criteria: iteratee(value, key, list)
  13. };
  14. }).sort(function(left, right) {
  15. var a = left.criteria;
  16. var b = right.criteria;
  17. if (a !== b) {
  18. if (a > b || a === void 0) return 1;
  19. if (a < b || b === void 0) return -1;
  20. }
  21. return left.index - right.index;
  22. }), 'value');
  23. }
  24. module.exports = sortBy;