You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
832 B

4 years ago
  1. 'use strict';
  2. var nestedValue = require('../helpers/nestedValue');
  3. var _require = require('../helpers/is'),
  4. isFunction = _require.isFunction;
  5. module.exports = function sortBy(valueOrFunction) {
  6. var collection = [].concat(this.items);
  7. var getValue = function getValue(item) {
  8. if (isFunction(valueOrFunction)) {
  9. return valueOrFunction(item);
  10. }
  11. return nestedValue(item, valueOrFunction);
  12. };
  13. collection.sort(function (a, b) {
  14. var valueA = getValue(a);
  15. var valueB = getValue(b);
  16. if (valueA === null || valueA === undefined) {
  17. return 1;
  18. }
  19. if (valueB === null || valueB === undefined) {
  20. return -1;
  21. }
  22. if (valueA < valueB) {
  23. return -1;
  24. }
  25. if (valueA > valueB) {
  26. return 1;
  27. }
  28. return 0;
  29. });
  30. return new this.constructor(collection);
  31. };