source: node_modules/core-js-pure/internals/array-sort.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.0 KB
Line 
1'use strict';
2var arraySlice = require('../internals/array-slice');
3
4var floor = Math.floor;
5
6var sort = function (array, comparefn) {
7 var length = array.length;
8
9 if (length < 8) {
10 // insertion sort
11 var i = 1;
12 var element, j;
13
14 while (i < length) {
15 j = i;
16 element = array[i];
17 while (j && comparefn(array[j - 1], element) > 0) {
18 array[j] = array[--j];
19 }
20 if (j !== i++) array[j] = element;
21 }
22 } else {
23 // merge sort
24 var middle = floor(length / 2);
25 var left = sort(arraySlice(array, 0, middle), comparefn);
26 var right = sort(arraySlice(array, middle), comparefn);
27 var llength = left.length;
28 var rlength = right.length;
29 var lindex = 0;
30 var rindex = 0;
31
32 while (lindex < llength || rindex < rlength) {
33 array[lindex + rindex] = (lindex < llength && rindex < rlength)
34 ? comparefn(left[lindex], right[rindex]) <= 0 ? left[lindex++] : right[rindex++]
35 : lindex < llength ? left[lindex++] : right[rindex++];
36 }
37 }
38
39 return array;
40};
41
42module.exports = sort;
Note: See TracBrowser for help on using the repository browser.