sort.js 409 B

123456789101112131415161718192021
  1. 'use strict';
  2. module.exports = function sort(fn) {
  3. var collection = [].concat(this.items);
  4. if (fn === undefined) {
  5. if (this.every(function (item) {
  6. return typeof item === 'number';
  7. })) {
  8. collection.sort(function (a, b) {
  9. return a - b;
  10. });
  11. } else {
  12. collection.sort();
  13. }
  14. } else {
  15. collection.sort(fn);
  16. }
  17. return new this.constructor(collection);
  18. };