Skip to content

Commit 21620a5

Browse files
committed
Merge pull request locutusjs#139 from codemasher/master
Fixed locutusjs#138 and did some cleanup
2 parents fd4268a + 297f040 commit 21620a5

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

functions/array/array_multisort.js

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,28 @@ function array_multisort(arr) {
1616
// bits: HGFE DCBA
1717
// args: Holds pointer to arguments for reassignment
1818

19-
var argl = arguments.length,
20-
sal = 0,
21-
flags = {
22-
'SORT_REGULAR': 16,
23-
'SORT_NUMERIC': 17,
24-
'SORT_STRING': 18,
25-
'SORT_ASC': 32,
26-
'SORT_DESC': 40
27-
},
28-
sortArrs = [
29-
[]
30-
],
31-
sortFlag = [0],
32-
sortKeys = [
33-
[]
34-
],
35-
g = 0,
36-
i = 0,
37-
j = 0,
38-
k = '',
39-
l = 0,
40-
thingsToSort = [],
41-
vkey = 0,
42-
zlast = null,
43-
args = arguments,
44-
nLastSort = [],
45-
lastSort = [],
46-
lastSorts = [],
47-
tmpArray = [],
48-
elIndex = 0,
49-
sortDuplicator = function(a, b) {
50-
return nLastSort.shift();
51-
};
19+
var g, i, j, k, l, sal, vkey, elIndex, lastSorts, tmpArray, zlast;
5220

53-
sortFunctions = [
54-
[
21+
var sortFlag = [0];
22+
var thingsToSort = [];
23+
var nLastSort = [];
24+
var lastSort = [];
25+
var args = arguments; // possibly redundant
5526

27+
var flags = {
28+
'SORT_REGULAR': 16,
29+
'SORT_NUMERIC': 17,
30+
'SORT_STRING': 18,
31+
'SORT_ASC': 32,
32+
'SORT_DESC': 40
33+
};
34+
35+
var sortDuplicator = function(a, b) {
36+
return nLastSort.shift();
37+
};
38+
39+
var sortFunctions = [
40+
[
5641
function(a, b) {
5742
lastSort.push(a > b ? 1 : (a < b ? -1 : 0));
5843
return a > b ? 1 : (a < b ? -1 : 0);
@@ -63,7 +48,6 @@ function array_multisort(arr) {
6348
}
6449
],
6550
[
66-
6751
function(a, b) {
6852
lastSort.push(a - b);
6953
return a - b;
@@ -74,7 +58,6 @@ function array_multisort(arr) {
7458
}
7559
],
7660
[
77-
7861
function(a, b) {
7962
lastSort.push((a + '') > (b + '') ? 1 : ((a + '') < (b + '') ? -1 : 0));
8063
return (a + '') > (b + '') ? 1 : ((a + '') < (b + '') ? -1 : 0);
@@ -86,6 +69,14 @@ function array_multisort(arr) {
8669
]
8770
];
8871

72+
var sortArrs = [
73+
[]
74+
];
75+
76+
var sortKeys = [
77+
[]
78+
];
79+
8980
// Store first argument into sortArrs and sortKeys if an Object.
9081
// First Argument should be either a Javascript Array or an Object, otherwise function would return FALSE like in PHP
9182
if (Object.prototype.toString.call(arr) === '[object Array]') {
@@ -104,10 +95,11 @@ function array_multisort(arr) {
10495
// arrMainLength: Holds the length of the first array. All other arrays must be of equal length, otherwise function would return FALSE like in PHP
10596
//
10697
// sortComponents: Holds 2 indexes per every section of the array that can be sorted. As this is the start, the whole array can be sorted.
107-
var arrMainLength = sortArrs[0].length,
108-
sortComponents = [0, arrMainLength];
98+
var arrMainLength = sortArrs[0].length;
99+
var sortComponents = [0, arrMainLength];
109100

110101
// Loop through all other arguments, checking lengths and sort flags of arrays and adding them to the above variables.
102+
var argl = arguments.length;
111103
for (j = 1; j < argl; j++) {
112104
if (Object.prototype.toString.call(arguments[j]) === '[object Array]') {
113105
sortArrs[j] = arguments[j];
@@ -130,7 +122,8 @@ function array_multisort(arr) {
130122
}
131123
} else if (typeof arguments[j] === 'string') {
132124
var lFlag = sortFlag.pop();
133-
if (typeof flags[arguments[j]] === 'undefined' || ((((flags[arguments[j]]) >>> 4) & (lFlag >>> 4)) > 0)) { // Keep extra parentheses around latter flags check to avoid minimization leading to CDATA closer
125+
// Keep extra parentheses around latter flags check to avoid minimization leading to CDATA closer
126+
if (typeof flags[arguments[j]] === 'undefined' || ((((flags[arguments[j]]) >>> 4) & (lFlag >>> 4)) > 0)) {
134127
return false;
135128
}
136129
sortFlag.push(lFlag + flags[arguments[j]]);
@@ -152,7 +145,7 @@ function array_multisort(arr) {
152145
nLastSort = [];
153146
lastSort = [];
154147

155-
// If ther are no sortComponents, then no more sorting is neeeded. Copy the array back to the argument.
148+
// If there are no sortComponents, then no more sorting is neeeded. Copy the array back to the argument.
156149
if (sortComponents.length === 0) {
157150
if (Object.prototype.toString.call(arguments[i]) === '[object Array]') {
158151
args[i] = sortArrs[i];

0 commit comments

Comments
 (0)