/functions/array/sort.js

https://gitlab.com/orvi2014/phpjs · JavaScript · 110 lines · 65 code · 7 blank · 38 comment · 18 complexity · 52507eff5b09897f1823fb40ee8f0899 MD5 · raw file

  1. function sort(inputArr, sort_flags) {
  2. // discuss at: http://phpjs.org/functions/sort/
  3. // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  4. // revised by: Brett Zamir (http://brett-zamir.me)
  5. // improved by: Brett Zamir (http://brett-zamir.me)
  6. // note: SORT_STRING (as well as natsort and natcasesort) might also be
  7. // note: integrated into all of these functions by adapting the code at
  8. // note: http://sourcefrog.net/projects/natsort/natcompare.js
  9. // note: This function deviates from PHP in returning a copy of the array instead
  10. // note: of acting by reference and returning true; this was necessary because
  11. // note: IE does not allow deleting and re-adding of properties without caching
  12. // note: of property position; you can set the ini of "phpjs.strictForIn" to true to
  13. // note: get the PHP behavior, but use this only if you are in an environment
  14. // note: such as Firefox extensions where for-in iteration order is fixed and true
  15. // note: property deletion is supported. Note that we intend to implement the PHP
  16. // note: behavior by default if IE ever does allow it; only gives shallow copy since
  17. // note: is by reference in PHP anyways
  18. // note: Since JS objects' keys are always strings, and (the
  19. // note: default) SORT_REGULAR flag distinguishes by key type,
  20. // note: if the content is a numeric string, we treat the
  21. // note: "original type" as numeric.
  22. // depends on: i18n_loc_get_default
  23. // example 1: var arr = ['Kevin', 'van', 'Zonneveld']
  24. // example 1: sort(arr);
  25. // example 1: $result = arr;
  26. // returns 1: ['Kevin', 'Zonneveld', 'van']
  27. // example 2: ini_set('phpjs.strictForIn', true);
  28. // example 2: fruits = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
  29. // example 2: sort(fruits);
  30. // example 2: $result = fruits;
  31. // returns 2: {0: 'apple', 1: 'banana', 2: 'lemon', 3: 'orange'}
  32. var valArr = [],
  33. keyArr = [],
  34. k = '',
  35. i = 0,
  36. sorter = false,
  37. that = this,
  38. strictForIn = false,
  39. populateArr = [];
  40. switch (sort_flags) {
  41. case 'SORT_STRING':
  42. // compare items as strings
  43. sorter = function(a, b) {
  44. return that.strnatcmp(a, b);
  45. };
  46. break;
  47. case 'SORT_LOCALE_STRING':
  48. // compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
  49. var loc = this.i18n_loc_get_default();
  50. sorter = this.php_js.i18nLocales[loc].sorting;
  51. break;
  52. case 'SORT_NUMERIC':
  53. // compare items numerically
  54. sorter = function(a, b) {
  55. return (a - b);
  56. };
  57. break;
  58. case 'SORT_REGULAR':
  59. // compare items normally (don't change types)
  60. default:
  61. sorter = function(a, b) {
  62. var aFloat = parseFloat(a),
  63. bFloat = parseFloat(b),
  64. aNumeric = aFloat + '' === a,
  65. bNumeric = bFloat + '' === b;
  66. if (aNumeric && bNumeric) {
  67. return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
  68. } else if (aNumeric && !bNumeric) {
  69. return 1;
  70. } else if (!aNumeric && bNumeric) {
  71. return -1;
  72. }
  73. return a > b ? 1 : a < b ? -1 : 0;
  74. };
  75. break;
  76. }
  77. // BEGIN REDUNDANT
  78. try {
  79. this.php_js = this.php_js || {};
  80. } catch (e) {
  81. this.php_js = {};
  82. }
  83. this.php_js.ini = this.php_js.ini || {};
  84. // END REDUNDANT
  85. strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js
  86. .ini['phpjs.strictForIn'].local_value !== 'off';
  87. populateArr = strictForIn ? inputArr : populateArr;
  88. for (k in inputArr) {
  89. // Get key and value arrays
  90. if (inputArr.hasOwnProperty(k)) {
  91. valArr.push(inputArr[k]);
  92. if (strictForIn) {
  93. delete inputArr[k];
  94. }
  95. }
  96. }
  97. valArr.sort(sorter);
  98. for (i = 0; i < valArr.length; i++) {
  99. // Repopulate the old array
  100. populateArr[i] = valArr[i];
  101. }
  102. return strictForIn || populateArr;
  103. }