Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ksort.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //对数组排序ascii
  2. export function ksort(inputArr, sort_flags) {
  3. var tmp_arr = {},
  4. keys = [],
  5. sorter, i, k, that = this,
  6. strictForIn = false,
  7. populateArr = {};
  8. let php_js = {};
  9. switch (sort_flags) {
  10. case 'SORT_STRING':
  11. // compare items as strings
  12. sorter = function (a, b) {
  13. return that.strnatcmp(a, b);
  14. };
  15. break;
  16. case 'SORT_LOCALE_STRING':
  17. // compare items as strings, original by the current locale (set with i18n_loc_set_default() as of PHP6)
  18. var loc = this.i18n_loc_get_default();
  19. sorter = php_js.i18nLocales[loc].sorting;
  20. break;
  21. case 'SORT_NUMERIC':
  22. // compare items numerically
  23. sorter = function (a, b) {
  24. return ((a + 0) - (b + 0));
  25. };
  26. break;
  27. // case 'SORT_REGULAR': // compare items normally (don't change types)
  28. default:
  29. sorter = function (a, b) {
  30. var aFloat = parseFloat(a),
  31. bFloat = parseFloat(b),
  32. aNumeric = aFloat + '' === a,
  33. bNumeric = bFloat + '' === b;
  34. if (aNumeric && bNumeric) {
  35. return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
  36. } else if (aNumeric && !bNumeric) {
  37. return 1;
  38. } else if (!aNumeric && bNumeric) {
  39. return -1;
  40. }
  41. return a > b ? 1 : a < b ? -1 : 0;
  42. };
  43. break;
  44. }
  45. // Make a list of key names
  46. for (k in inputArr) {
  47. if (inputArr.hasOwnProperty(k)) {
  48. keys.push(k);
  49. }
  50. }
  51. keys.sort(sorter);
  52. // BEGIN REDUNDANT
  53. php_js = php_js || {};
  54. php_js.ini = php_js.ini || {};
  55. // END REDUNDANT
  56. strictForIn = php_js.ini['phpjs.strictForIn'] && php_js.ini['phpjs.strictForIn'].local_value && php_js
  57. .ini['phpjs.strictForIn'].local_value !== 'off';
  58. populateArr = strictForIn ? inputArr : populateArr;
  59. // Rebuild array with sorted key names
  60. for (i = 0; i < keys.length; i++) {
  61. k = keys[i];
  62. tmp_arr[k] = inputArr[k];
  63. if (strictForIn) {
  64. delete inputArr[k];
  65. }
  66. }
  67. for (i in tmp_arr) {
  68. if (tmp_arr.hasOwnProperty(i)) {
  69. populateArr[i] = tmp_arr[i];
  70. }
  71. }
  72. return strictForIn || populateArr;
  73. }
  74. // module.exports = {
  75. // ksort: ksort
  76. // }