JavaScript krsort
Sort an array by key value in reverse order
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 | function krsort (inputArr, sort_flags) { // Sort an array by key value in reverse order // // version: 1008.1718 // discuss at: http://phpjs.org/functions/krsort // + original by: GeekFG (http://geekfg.blogspot.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Brett Zamir (http://brett-zamir.me) // % note 1: The examples are correct, this is a new way // % note 2: This function deviates from PHP in returning a copy of the array instead // % note 2: of acting by reference and returning true; this was necessary because // % note 2: IE does not allow deleting and re-adding of properties without caching // % note 2: of property position; you can set the ini of "phpjs.strictForIn" to true to // % note 2: get the PHP behavior, but use this only if you are in an environment // % note 2: such as Firefox extensions where for-in iteration order is fixed and true // % note 2: property deletion is supported. Note that we intend to implement the PHP // % note 2: behavior by default if IE ever does allow it; only gives shallow copy since // % note 2: is by reference in PHP anyways // - depends on: i18n_loc_get_default // * example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'}; // * example 1: data = krsort(data); // * results 1: {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'} // * example 2: ini_set('phpjs.strictForIn', true); // * example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'}; // * example 2: krsort(data); // * results 2: data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'} // * returns 2: true var tmp_arr={}, keys=[], sorter, i, k, that=this, strictForIn = false, populateArr = {}; switch (sort_flags) { case 'SORT_STRING': // compare items as strings sorter = function (a, b) { return that.strnatcmp(b, a); }; break; case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6) var loc = this.i18n_loc_get_default(); sorter = this.php_js.i18nLocales[loc].sorting; break; case 'SORT_NUMERIC': // compare items numerically sorter = function (a, b) { return (b - a); }; break; case 'SORT_REGULAR': // compare items normally (don't change types) default: sorter = function (a, b) { if (a < b) { return 1; } if (a > b) { return -1; } return 0; }; break; } // Make a list of key names for (k in inputArr) { if (inputArr.hasOwnProperty(k)) { keys.push(k); } } keys.sort(sorter); // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.ini = this.php_js.ini || {}; // END REDUNDANT strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js.ini['phpjs.strictForIn'].local_value !== 'off'; populateArr = strictForIn ? inputArr : populateArr; // Rebuild array with sorted key names for (i = 0; i < keys.length; i++) { k = keys[i]; tmp_arr[k] = inputArr[k]; if (strictForIn) { delete inputArr[k]; } } for (i in tmp_arr) { if (tmp_arr.hasOwnProperty(i)) { populateArr[i] = tmp_arr[i]; } } return strictForIn || populateArr; } |
Examples
» Example 1
Running
1 2 | data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'}; data = krsort(data); |
Should result in
1 | {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'} |
» Example 2
Running
1 2 3 | ini_set('phpjs.strictForIn', true); data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'}; krsort(data); |
Should result in
1 | data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'} |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have krsort goodness in JavaScript.
Considering the last story about duplication... I'm struggling with the same problem trying to include the BCMath functions
http://svn.plutonia.nl/projects/phpjs/browser/trunk/_unported/bc
Which were donated in my mail by a guy named Lance. They all require a shared bc library with 10 functions or something.
There is no global required include in PHP.JS that would be the obvious place for these shared functions. And I'm still convinced that this library should go without such a requirement.
Still this will mean, either:
OR
- awfully big standalone functions, extreme duplication
- a compiler that defeats duplication. from a developer's perspective, at least.
- a core include with shared functions that is required as a dependency
- make use of the existing dependency system. that would mean adding non-existent-php functions as if they were real php functions and adding them as dependencies. We could store them in a category: phpjssupport. But that would require creating exceptions throughout the project, e.g.: 'SKIP phpjssupport dir when downloading manuals!'
- no bcmath functions at all
It's an imperfect world.
@ Brett Zamir: Again: great work.
It's a shame we don't have a php.core.js in which we could just have an array_sorter() with some arguments to diversify in ksort krsort asort arsort, etc.
Same goes for all the intersect & diff functions that only differ a couple of characters. More (maintenance) work for us due to duplication.
But on the other hand: I think it's worth it because we can uphold the philosophy of a lot of independent components that can just be used as is. I bet the majority of PHP.JS users just copy the functions they need and be on their way.
We could work on a compiler-like feature that just generates all the different sorters based on one template we maintain. But concidering we're only talking about 20 functions or so: that's maybe more work than just distributing changes by hand (copy&paste). Thanks Brett. You're a real PHP.JS pitbull :)
And here are krsort() and ksort() with the new more readily extensible infrastructure (and speeded krsort()):
[CODE="Javascript"]function krsort(array, sort_flags) {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir
// % note: The examples are correct, this is a new way
// * example 1: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
// * example 1: krsort(data);
// * results 1: data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'}
// * returns 1: true
var tmp_arr={}, keys=[], sorter, i, key;
switch (sort_flags) {
case 'SORT_STRING': // compare items as strings
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
throw 'Not implemented yet';
case 'SORT_NUMERIC': // compare items numerically
sorter = function (a, b) {
return(b - a);
};
break;
case 'SORT_REGULAR': // compare items normally (don't change types)
default:
sorter = function (a, b) {
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
};
break;
}
// Make a list of key names
for (key in array) {
keys.push(key);
}
keys.sort(sorter);
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
key = keys[i];
tmp_arr[key] = array[key];
delete array[key];
}
for (i in tmp_arr) {
array[i] = tmp_arr[i]
}
return true;
}
function ksort(array, sort_flags) {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir
// % note: The examples are correct, this is a new way
// * example 1: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
// * example 1: ksort(data);
// * results 1: data == {1: 'Kevin', 2: 'van', 3: 'Zonneveld'}
// * returns 1: true
var tmp_arr={}, keys=[], sorter, i, key;
switch (sort_flags) {
case 'SORT_STRING': // compare items as strings
case 'SORT_LOCALE_STRING': // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
throw 'Not implemented yet';
case 'SORT_NUMERIC': // compare items numerically
sorter = function (a, b) {
return(a - b);
};
break;
case 'SORT_REGULAR': // compare items normally (don't change types)
default:
sorter = function (a, b) {
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
};
break;
}
// Make a list of key names
for (key in array) {
keys.push(key);
}
keys.sort(sorter);
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
key = keys[i];
tmp_arr[key] = array[key];
delete array[key];
}
for (i in tmp_arr) {
array[i] = tmp_arr[i]
}
return true;
}[/CODE]
I found that init'ing tmp_arr to {} was problematic and changed it to var tmp_arr = [].
Specifically I was unable to get the length of the object... this change doesn't seem to have harmed the function at all.
Also it may stray from the PHP manual a bit but I found it more useful to return the array than to return true, because the original array wasn't being overwritten in my case. I'm using server-side JScript with Classic ASP.


Brett Zamir
18 Jan '09
Just specify code like this with each relevant function:
[CODE="Javascript"]if (!window.php_js) {
window.php_js = {};
}
if (!window.php_js.output_buffer) {
window.php_js.output_buffer = document.createDocumentFragment();
}[/CODE]
or
[CODE="Javascript"]if (!window.php_js) {
window.php_js = {};
}
if (!window.php_js.sort_helper1) {
window.php_js.sort_helper1 = function () {
...
}
}[/CODE]
The above could be inside or outside the functions. Outside would be easier to ignore and avoid repeated execution, but inside might be more user friendly and would only increase space if the user didn't realize they could omit it in repeat cases, but should barely affect execution time, since the functions would only be declared once.
I think we do need something like this (whatever option you like) because it would just get out of hand for memory and execution to include these fully and without conditionality within each function.