No Description

sortBy.js 680B

123456789101112131415161718192021222324
  1. import cb from './_cb.js';
  2. import pluck from './pluck.js';
  3. import map from './map.js';
  4. // Sort the object's values by a criterion produced by an iteratee.
  5. export default 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. }