JavaScript rsort
Sort an array 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 | function rsort (inputArr, sort_flags) { // Sort an array in reverse order // // version: 912.1315 // discuss at: http://phpjs.org/functions/rsort // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: Brett Zamir (http://brett-zamir.me) // + improved by: Brett Zamir (http://brett-zamir.me) // % note 1: SORT_STRING (as well as natsort and natcasesort) might also be // % note 1: integrated into all of these functions by adapting the code at // % note 1: http://sourcefrog.net/projects/natsort/natcompare.js // % 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: rsort(['Kevin', 'van', 'Zonneveld']); // * returns 1: ['van', 'Zonneveld', 'Kevin'] // * example 2: ini_set('phpjs.strictForIn', true); // * example 2: fruits = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'}; // * example 2: rsort(fruits); // * results 2: fruits == {0: 'orange', 1: 'lemon', 2: 'banana', 3: 'apple'} // * returns 2: true var valArr = [], k = '', i = 0, sorter = false, 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; } // 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; populateArr = strictForIn ? inputArr : populateArr; for (k in inputArr) { // Get key and value arrays if (inputArr.hasOwnProperty) { valArr.push(inputArr[k]); if (strictForIn) { delete inputArr[k]; } } } valArr.sort(sorter); for (i = 0; i < valArr.length; i++) { // Repopulate the old array populateArr[i] = valArr[i]; } return strictForIn ? true : populateArr; } |
Examples
» Example 1
Running
1 | rsort(['Kevin', 'van', 'Zonneveld']); |
Should return
1 | ['van', 'Zonneveld', 'Kevin'] |
» Example 2
Running
1 2 3 | ini_set('phpjs.strictForIn', true); fruits = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'}; rsort(fruits); |
Should result in
1 | fruits == {0: 'orange', 1: 'lemon', 2: 'banana', 3: 'apple'} |
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 rsort goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>