21 lines
409 B
JavaScript
21 lines
409 B
JavaScript
|
'use strict';
|
||
|
|
||
|
module.exports = function sort(fn) {
|
||
|
var collection = [].concat(this.items);
|
||
|
|
||
|
if (fn === undefined) {
|
||
|
if (this.every(function (item) {
|
||
|
return typeof item === 'number';
|
||
|
})) {
|
||
|
collection.sort(function (a, b) {
|
||
|
return a - b;
|
||
|
});
|
||
|
} else {
|
||
|
collection.sort();
|
||
|
}
|
||
|
} else {
|
||
|
collection.sort(fn);
|
||
|
}
|
||
|
|
||
|
return new this.constructor(collection);
|
||
|
};
|