PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/_octopress/source/functions/ksort/index.markdown

https://gitlab.com/orvi2014/phpjs
Markdown | 140 lines | 127 code | 13 blank | 0 comment | 0 complexity | 7787c96877733b6b3e9457bd348e42c6 MD5 | raw file
  1. ---
  2. layout: page
  3. title: "JavaScript ksort function"
  4. comments: true
  5. sharing: true
  6. footer: true
  7. alias:
  8. - /functions/view/ksort:460
  9. - /functions/view/ksort
  10. - /functions/view/460
  11. - /functions/ksort:460
  12. - /functions/460
  13. ---
  14. <!-- Generated by Rakefile:build -->
  15. A JavaScript equivalent of PHP's ksort
  16. {% codeblock array/ksort.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/ksort.js raw on github %}
  17. function ksort(inputArr, sort_flags) {
  18. // discuss at: http://phpjs.org/functions/ksort/
  19. // original by: GeekFG (http://geekfg.blogspot.com)
  20. // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  21. // improved by: Brett Zamir (http://brett-zamir.me)
  22. // note: The examples are correct, this is a new way
  23. // note: This function deviates from PHP in returning a copy of the array instead
  24. // note: of acting by reference and returning true; this was necessary because
  25. // note: IE does not allow deleting and re-adding of properties without caching
  26. // note: of property position; you can set the ini of "phpjs.strictForIn" to true to
  27. // note: get the PHP behavior, but use this only if you are in an environment
  28. // note: such as Firefox extensions where for-in iteration order is fixed and true
  29. // note: property deletion is supported. Note that we intend to implement the PHP
  30. // note: behavior by default if IE ever does allow it; only gives shallow copy since
  31. // note: is by reference in PHP anyways
  32. // note: Since JS objects' keys are always strings, and (the
  33. // note: default) SORT_REGULAR flag distinguishes by key type,
  34. // note: if the content is a numeric string, we treat the
  35. // note: "original type" as numeric.
  36. // depends on: i18n_loc_get_default
  37. // depends on: strnatcmp
  38. // example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
  39. // example 1: data = ksort(data);
  40. // example 1: $result = data
  41. // returns 1: {a: 'orange', b: 'banana', c: 'apple', d: 'lemon'}
  42. // example 2: ini_set('phpjs.strictForIn', true);
  43. // example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
  44. // example 2: ksort(data);
  45. // example 2: $result = data
  46. // returns 2: {1: 'Kevin', 2: 'van', 3: 'Zonneveld'}
  47. var tmp_arr = {},
  48. keys = [],
  49. sorter, i, k, that = this,
  50. strictForIn = false,
  51. populateArr = {};
  52. switch (sort_flags) {
  53. case 'SORT_STRING':
  54. // compare items as strings
  55. sorter = function(a, b) {
  56. return that.strnatcmp(a, b);
  57. };
  58. break;
  59. case 'SORT_LOCALE_STRING':
  60. // compare items as strings, original by the current locale (set with i18n_loc_set_default() as of PHP6)
  61. var loc = this.i18n_loc_get_default();
  62. sorter = this.php_js.i18nLocales[loc].sorting;
  63. break;
  64. case 'SORT_NUMERIC':
  65. // compare items numerically
  66. sorter = function(a, b) {
  67. return ((a + 0) - (b + 0));
  68. };
  69. break;
  70. // case 'SORT_REGULAR': // compare items normally (don't change types)
  71. default:
  72. sorter = function(a, b) {
  73. var aFloat = parseFloat(a),
  74. bFloat = parseFloat(b),
  75. aNumeric = aFloat + '' === a,
  76. bNumeric = bFloat + '' === b;
  77. if (aNumeric && bNumeric) {
  78. return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
  79. } else if (aNumeric && !bNumeric) {
  80. return 1;
  81. } else if (!aNumeric && bNumeric) {
  82. return -1;
  83. }
  84. return a > b ? 1 : a < b ? -1 : 0;
  85. };
  86. break;
  87. }
  88. // Make a list of key names
  89. for (k in inputArr) {
  90. if (inputArr.hasOwnProperty(k)) {
  91. keys.push(k);
  92. }
  93. }
  94. keys.sort(sorter);
  95. // BEGIN REDUNDANT
  96. this.php_js = this.php_js || {};
  97. this.php_js.ini = this.php_js.ini || {};
  98. // END REDUNDANT
  99. strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js
  100. .ini['phpjs.strictForIn'].local_value !== 'off';
  101. populateArr = strictForIn ? inputArr : populateArr;
  102. // Rebuild array with sorted key names
  103. for (i = 0; i < keys.length; i++) {
  104. k = keys[i];
  105. tmp_arr[k] = inputArr[k];
  106. if (strictForIn) {
  107. delete inputArr[k];
  108. }
  109. }
  110. for (i in tmp_arr) {
  111. if (tmp_arr.hasOwnProperty(i)) {
  112. populateArr[i] = tmp_arr[i];
  113. }
  114. }
  115. return strictForIn || populateArr;
  116. }
  117. {% endcodeblock %}
  118. - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/ksort.js)
  119. Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
  120. the closest match to this hashtable-like data structure.
  121. Please also note that php.js offers community built functions and goes by the
  122. [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
  123. functions that are far from perfect, in the hopes to spark better contributions.
  124. Do you have one? Then please just:
  125. - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/ksort.js)
  126. ### Other PHP functions in the array extension
  127. {% render_partial _includes/custom/array.html %}