Skip to content

Commit 35e048d

Browse files
committed
Many improvements
1 parent 542e2c1 commit 35e048d

File tree

13 files changed

+265
-11
lines changed

13 files changed

+265
-11
lines changed

_tools/t.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
//echo mktime(0, 0, 0, 1, 1, 1998)."\n";
77

88

9-
echo mktime(0, 0, 0, 0, 1, 2008)."\n";
9+
echo strnatcasecmp("10", "1")."\n";
10+
echo strnatcasecmp(10, 1)."\n";
11+
echo strnatcasecmp('1', '10')."\n";
1012
//$x = get_html_translation_table(HTML_ENTITIES);
1113
//print_r($x);
12-
?>
14+
?>
15+
echo strnatcasecmp(10, 1)."\n";

_unported/array/natcasesort.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

_unported/array/natsort.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

_unported/strings/strnatcasecmp.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

functions/array/arsort.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ function arsort(inputArr, sort_flags) {
1616

1717
switch (sort_flags) {
1818
case 'SORT_STRING': // compare items as strings
19+
sorter = function (a, b) {
20+
return strnatcmp(b, a);
21+
};
22+
break;
1923
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
20-
throw 'Not implemented yet';
24+
sorter = function (a, b) {
25+
return(b.localeCompare(a));
26+
};
27+
break;
2128
case 'SORT_NUMERIC': // compare items numerically
2229
sorter = function (a, b) {
2330
return(a - b);

functions/array/asort.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function asort(inputArr, sort_flags) {
77
// % note 1: http://sourcefrog.net/projects/natsort/natcompare.js
88
// % note 2: The examples are correct, this is a new way
99
// % note 2: Credits to: http://javascript.internet.com/math-related/bubble-sort.html
10+
// - depends on: strnatcmp
1011
// * example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
1112
// * example 1: asort(data);
1213
// * results 1: data == {c: 'apple', b: 'banana', d: 'lemon', a: 'orange'}
@@ -16,8 +17,15 @@ function asort(inputArr, sort_flags) {
1617

1718
switch (sort_flags) {
1819
case 'SORT_STRING': // compare items as strings
20+
sorter = function (a, b) {
21+
return strnatcmp(a, b);
22+
};
23+
break;
1924
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
20-
throw 'Not implemented yet';
25+
sorter = function (a, b) {
26+
return(a.localeCompare(b));
27+
};
28+
break;
2129
case 'SORT_NUMERIC': // compare items numerically
2230
sorter = function (a, b) {
2331
return(a - b);

functions/array/krsort.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ function krsort(array, sort_flags) {
1313

1414
switch (sort_flags) {
1515
case 'SORT_STRING': // compare items as strings
16+
sorter = function (a, b) {
17+
return strnatcmp(b, a);
18+
};
19+
break;
1620
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
17-
throw 'Not implemented yet';
21+
sorter = function (a, b) {
22+
return(b.localeCompare(a));
23+
};
24+
break;
1825
case 'SORT_NUMERIC': // compare items numerically
1926
sorter = function (a, b) {
2027
return(b - a);

functions/array/ksort.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,15 @@ function ksort(array, sort_flags) {
1313

1414
switch (sort_flags) {
1515
case 'SORT_STRING': // compare items as strings
16+
sorter = function (a, b) {
17+
return strnatcmp(a, b);
18+
};
19+
break;
1620
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
17-
throw 'Not implemented yet';
21+
sorter = function (a, b) {
22+
return(a.localeCompare(b));
23+
};
24+
break;
1825
case 'SORT_NUMERIC': // compare items numerically
1926
sorter = function (a, b) {
2027
return(a - b);

functions/array/natcasesort.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function natcasesort(inputArr) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir
4+
// - depends on: strnatcasecmp
5+
// * example 1: $array1 = {0:'IMG0.png', 1:'img12.png', 2:'img10.png', 3:'img2.png', 4:'img1.png', 5:'IMG3.png'};
6+
// * example 1: natcasesort($array1);
7+
// * returns 1: {0: 'IMG0.png', 4: 'img1.png', 3: 'img2.png', 5: 'IMG3.png', 2: 'img10.png', 1: 'img12.png'}
8+
9+
var valArr=[], keyArr=[], k, i, ret;
10+
11+
var bubbleSort = function(keyArr, inputArr) {
12+
var i, j, tempValue, tempKeyVal;
13+
for (i = inputArr.length-2; i >= 0; i--) {
14+
for (j = 0; j <= i; j++) {
15+
ret = strnatcasecmp(inputArr[j+1], inputArr[j]);
16+
if (ret < 0) {
17+
tempValue = inputArr[j];
18+
inputArr[j] = inputArr[j+1];
19+
inputArr[j+1] = tempValue;
20+
tempKeyVal = keyArr[j];
21+
keyArr[j] = keyArr[j+1];
22+
keyArr[j+1] = tempKeyVal;
23+
}
24+
}
25+
}
26+
};
27+
28+
// Get key and value arrays
29+
for (k in inputArr) {
30+
valArr.push(inputArr[k]);
31+
keyArr.push(k);
32+
delete inputArr[k] ;
33+
}
34+
try {
35+
// Sort our new temporary arrays
36+
bubbleSort(keyArr, valArr);
37+
} catch(e) {
38+
return false;
39+
}
40+
41+
// Repopulate the old array
42+
for (i = 0; i < valArr.length; i++) {
43+
inputArr[keyArr[i]] = valArr[i];
44+
}
45+
46+
return true;
47+
}

functions/array/natsort.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function natsort(inputArr) {
2+
// http://kevin.vanzonneveld.net
3+
// + original by: Brett Zamir
4+
// - depends on: strnatcmp
5+
// * example 1: $array1 = {a:"img12.png", b:"img10.png", c:"img2.png", d:"img1.png"};
6+
// * example 1: natcasesort($array1);
7+
// * returns 1: {d: 'img1.png', c: 'img2.png', b: 'img10.png', a: 'img12.png'}
8+
9+
var valArr=[], keyArr=[], k, i, ret;
10+
11+
var bubbleSort = function(keyArr, inputArr) {
12+
var i, j, tempValue, tempKeyVal;
13+
for (i = inputArr.length-2; i >= 0; i--) {
14+
for (j = 0; j <= i; j++) {
15+
ret = strnatcmp(inputArr[j+1], inputArr[j]);
16+
if (ret < 0) {
17+
tempValue = inputArr[j];
18+
inputArr[j] = inputArr[j+1];
19+
inputArr[j+1] = tempValue;
20+
tempKeyVal = keyArr[j];
21+
keyArr[j] = keyArr[j+1];
22+
keyArr[j+1] = tempKeyVal;
23+
}
24+
}
25+
}
26+
};
27+
28+
// Get key and value arrays
29+
for (k in inputArr) {
30+
valArr.push(inputArr[k]);
31+
keyArr.push(k);
32+
delete inputArr[k] ;
33+
}
34+
try {
35+
// Sort our new temporary arrays
36+
bubbleSort(keyArr, valArr);
37+
} catch(e) {
38+
return false;
39+
}
40+
41+
// Repopulate the old array
42+
for (i = 0; i < valArr.length; i++) {
43+
inputArr[keyArr[i]] = valArr[i];
44+
}
45+
46+
return true;
47+
}

0 commit comments

Comments
 (0)