UNPKG

931 BJavaScriptView Raw
1'use strict';
2var $ = require('../internals/export');
3var aFunction = require('../internals/a-function');
4var toObject = require('../internals/to-object');
5var fails = require('../internals/fails');
6var sloppyArrayMethod = require('../internals/sloppy-array-method');
7
8var nativeSort = [].sort;
9var test = [1, 2, 3];
10
11// IE8-
12var FAILS_ON_UNDEFINED = fails(function () {
13 test.sort(undefined);
14});
15// V8 bug
16var FAILS_ON_NULL = fails(function () {
17 test.sort(null);
18});
19// Old WebKit
20var SLOPPY_METHOD = sloppyArrayMethod('sort');
21
22var FORCED = FAILS_ON_UNDEFINED || !FAILS_ON_NULL || SLOPPY_METHOD;
23
24// `Array.prototype.sort` method
25// https://tc39.github.io/ecma262/#sec-array.prototype.sort
26$({ target: 'Array', proto: true, forced: FORCED }, {
27 sort: function sort(comparefn) {
28 return comparefn === undefined
29 ? nativeSort.call(toObject(this))
30 : nativeSort.call(toObject(this), aFunction(comparefn));
31 }
32});