????? ????????
on 2012-04-04 14:32:54
Great job here. I really enjoyed what you had to say. Keep going because you definitely bring a new voice to this subject. Not many people would say what you’ve said and still make it interesting
Kevin van Zonneveld
on 2009-01-16 23:48:30
@ Brett Zamir: What a beautiful moment it is, when I paste one of your functions, write a testcase, click on 'Play', and see:
Test result
===========================================================================
array/uasort.js results#1 OKAY
That word 'OKAY' is like a little present :) You are awesome Brett.
Brett Zamir
on 2009-01-16 13:10:05
Grrrr. Sorry, the 'i' for loop (in sort(), rsort(), and usort() need to have 'var' added in front)--accidental globals... Thx, Brett
Brett Zamir
on 2009-01-16 13:05:41
Actually, please just use this one for usort() instead, as I needed to allow the callback as string. There's also another array implementation: uasort().
function usort (inputArr, sorter) {
if (typeof sorter === 'string') {
sorter = this[sorter];
}
else if (sorter instanceof Array) {
sorter = this[sorter[0]][sorter[1]];
}
var valArr = [], keyArr=[];
for (var k in inputArr) { // Get key and value arrays
valArr.push(inputArr[k]);
delete inputArr[k] ;
}
try {
valArr.sort(sorter);
} catch (e) {
return false;
}
for (i=0; i < valArr.length; i++) { // Repopulate the old array
inputArr[i] = valArr[i];
}
return true;
}
$stuff = {"d" : "3", "a" : "1", "b" : "11", "c" : "4"};
usort($stuff, function (a, b) {
return(a-b);
});
function uasort (inputArr, sorter) {
if (typeof sorter === 'string') {
sorter = this[sorter];
}
else if (sorter instanceof Array) {
sorter = this[sorter[0]][sorter[1]];
}
var valArr = [], keyArr=[], tempKeyVal, tempValue, ret;
var sorterNew = function (keyArr, valArr) {
for (var i=valArr.length-2; i >=0; i--) {
for (var j=0; j <= i; j++) {
ret = sorter(valArr[j+1], valArr[j]);
if (ret < 0) {
tempValue = valArr[j];
valArr[j] = valArr[j+1];
valArr[j+1] = tempValue;
tempKeyVal = keyArr[j];
keyArr[j] = keyArr[j+1];
keyArr[j+1] = tempKeyVal;
}
}
}
}
for (var k in inputArr) { // Get key and value arrays
valArr.push(inputArr[k]);
keyArr.push(k);
delete inputArr[k] ;
}
try {
sorterNew(keyArr, valArr); // Sort our new temporary arrays
}
catch(e) {
return false;
}
for (i=0; i < valArr.length; i++) { // Repopulate the old array
inputArr[keyArr[i]] = valArr[i];
}
return true;
}
$fruits = {"d" : "lemon", "a" : "orange", "b" : "banana", "c" : "apple"};
uasort($fruits, function (a, b) {
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
});
var $output = '';
for (var $key in $fruits) {
$val = $fruits[$key];
$output += $key+' = '+$val+"\n";
}
alert($output);
/*
c = apple
b = banana
d = lemon
a = orange
*/
Brett Zamir
on 2009-01-16 11:54:44
Sorry, forgot to rename the last one to usort()!
Brett Zamir
on 2009-01-16 11:52:56
Here's sort(), rsort(), and usort() which now support objects (though no additions of sorting types for sort() or rsort()):
function sort (inputArr, sort_flags) {
var valArr = [], keyArr=[];
for (var k in inputArr) { // Get key and value arrays
valArr.push(inputArr[k]);
delete inputArr[k] ;
}
var sorter = false;
// For now only SORT_NUMERIC has a custom sorter
// and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING
// are all handled with the default sorter
if (sort_flags === 'SORT_NUMERIC') {
sorter = function (a, b) {
return(a - b);
};
}
if (sorter !== false) {
valArr.sort(sorter);
} else {
valArr.sort();
}
for (i=0; i < valArr.length; i++) { // Repopulate the old array
inputArr[i] = valArr[i];
}
return true;
}
$fruits = {"d" : "lemon", "a" : "orange", "b" : "banana", "c" : "apple"};
sort($fruits);
var $output = '';
for (var $key in $fruits) {
$val = $fruits[$key];
$output += $key+' = '+$val+"\n";
}
alert($output);
/*
0 = apple
1 = banana
2 = lemon
3 = orange
*/
function rsort (inputArr, sort_flags) {
var valArr = [], keyArr=[];
for (var k in inputArr) { // Get key and value arrays
valArr.push(inputArr[k]);
delete inputArr[k] ;
}
var sorter = false;
// For now only SORT_NUMERIC has a custom sorter
// and SORT_REGULAR, SORT_STRING, and SORT_LOCALE_STRING
// are all handled with the default sorter
if (sort_flags === 'SORT_NUMERIC') {
sorter = function (a, b) {
return(b - a);
};
}
if (sorter !== false) {
valArr.sort(sorter);
} else {
valArr.sort();
valArr.reverse();
}
for (i=0; i < valArr.length; i++) { // Repopulate the old array
inputArr[i] = valArr[i];
}
return true;
}
$fruits = {"d" : "lemon", "a" : "orange", "b" : "banana", "c" : "apple"};
//$fruits = ["lemon", "orange", "banana", "apple"];
// $fruits = {"d" : "3", "a" : "1", "b" : "11", "c" : "4"};
rsort($fruits);
// rsort($fruits, 'SORT_NUMERIC');
var $output = '';
for (var $key in $fruits) {
$val = $fruits[$key];
$output += $key+' = '+$val+"\n";
}
alert($output);
/*
0 = orange
1 = lemon
2 = banana
3 = apple
*/
function sort (inputArr, sorter) {
var valArr = [], keyArr=[];
for (var k in inputArr) { // Get key and value arrays
valArr.push(inputArr[k]);
delete inputArr[k] ;
}
try {
valArr.sort(sorter);
} catch (e) {
return false;
}
for (i=0; i < valArr.length; i++) { // Repopulate the old array
inputArr[i] = valArr[i];
}
return true;
}
$stuff = {"d" : "3", "a" : "1", "b" : "11", "c" : "4"};
sort($stuff, function (a, b) {
return(a-b);
});
var $output = '';
for (var $key in $stuff) {
$val = $stuff[$key];
$output += $key+' = '+$val+"\n";
}
alert($output);
/*
0 = 1
1 = 3
2 = 4
3 = 11
*/