SortService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 21/11/23
  6. * Time: 19:05.
  7. */
  8. namespace App\Services\Api;
  9. class SortService
  10. {
  11. // $list 二维数组
  12. public static function getResultList($list)
  13. {
  14. return self::groupByFirstLetter($list);
  15. }
  16. protected static function groupByFirstLetter($list)
  17. {
  18. $r_list = [];
  19. foreach ($list as $key => $val) {
  20. $letter = self::firstChar($val['name']);
  21. $val['letter'] = $letter;
  22. $r_list[$letter][] = $val;
  23. }
  24. foreach ($r_list as $k => $v) {
  25. $r_list[$k] = self::listSortBy($v, 'name');
  26. }
  27. ksort($r_list);
  28. $r_list = array_values($r_list);
  29. return $r_list;
  30. }
  31. protected static function listSortBy($list, $field)
  32. {
  33. $arr = $result = [];
  34. foreach ($list as $key => $val) {
  35. $arr[$key] = &$val[$field];
  36. }
  37. asort($arr);
  38. foreach ($arr as $k => $v) {
  39. $result[] = &$list[$k];
  40. }
  41. return $result;
  42. }
  43. public static function firstChar($s)
  44. {
  45. $s0 = mb_substr($s, 0, 3); // 获取名字的姓
  46. $s = iconv('UTF-8', 'gb2312', $s0); // 将UTF-8转换成GB2312编码
  47. if (ord($s0) > 128) { // 汉字开头,汉字没有以U、V开头的
  48. $asc = ord($s[0]) * 256 + ord($s[0]) - 65536;
  49. if ($asc >= -20319 and $asc <= -20284) {
  50. return 'A';
  51. }
  52. if ($asc >= -20283 and $asc <= -19776) {
  53. return 'B';
  54. }
  55. if ($asc >= -19775 and $asc <= -19219) {
  56. return 'C';
  57. }
  58. if ($asc >= -19218 and $asc <= -18711) {
  59. return 'D';
  60. }
  61. if ($asc >= -18710 and $asc <= -18527) {
  62. return 'E';
  63. }
  64. if ($asc >= -18526 and $asc <= -18240) {
  65. return 'F';
  66. }
  67. if ($asc >= -18239 and $asc <= -17760) {
  68. return 'G';
  69. }
  70. if ($asc >= -17759 and $asc <= -17248) {
  71. return 'H';
  72. }
  73. if ($asc >= -17247 and $asc <= -17418) {
  74. return 'I';
  75. }
  76. if ($asc >= -17417 and $asc <= -16475) {
  77. return 'J';
  78. }
  79. if ($asc >= -16474 and $asc <= -16213) {
  80. return 'K';
  81. }
  82. if ($asc >= -16212 and $asc <= -15641) {
  83. return 'L';
  84. }
  85. if ($asc >= -15640 and $asc <= -15166) {
  86. return 'M';
  87. }
  88. if ($asc >= -15165 and $asc <= -14923) {
  89. return 'N';
  90. }
  91. if ($asc >= -14922 and $asc <= -14915) {
  92. return 'O';
  93. }
  94. if ($asc >= -14914 and $asc <= -14631) {
  95. return 'P';
  96. }
  97. if ($asc >= -14630 and $asc <= -14150) {
  98. return 'Q';
  99. }
  100. if ($asc >= -14149 and $asc <= -14091) {
  101. return 'R';
  102. }
  103. if ($asc >= -14090 and $asc <= -13319) {
  104. return 'S';
  105. }
  106. if ($asc >= -13318 and $asc <= -12839) {
  107. return 'T';
  108. }
  109. if ($asc >= -12838 and $asc <= -12557) {
  110. return 'W';
  111. }
  112. if ($asc >= -12556 and $asc <= -11848) {
  113. return 'X';
  114. }
  115. if ($asc >= -11847 and $asc <= -11056) {
  116. return 'Y';
  117. }
  118. if ($asc >= -11055 and $asc <= -10247) {
  119. return 'Z';
  120. }
  121. } elseif (ord($s) >= 48 and ord($s) <= 57) { // 数字开头
  122. switch (iconv_substr($s, 0, 1, 'utf-8')) {
  123. case 1:
  124. return 'Y';
  125. case 2:
  126. return 'E';
  127. case 3:
  128. return 'S';
  129. case 4:
  130. return 'S';
  131. case 5:
  132. return 'W';
  133. case 6:
  134. return 'L';
  135. case 7:
  136. return 'Q';
  137. case 8:
  138. return 'B';
  139. case 9:
  140. return 'J';
  141. case 0:
  142. return 'L';
  143. }
  144. } elseif (ord($s) >= 65 and ord($s) <= 90) { // 大写英文开头
  145. return substr($s, 0, 1);
  146. } elseif (ord($s) >= 97 and ord($s) <= 122) { // 小写英文开头
  147. return strtoupper(substr($s, 0, 1));
  148. } else {
  149. return iconv_substr($s0, 0, 1, 'utf-8');
  150. }
  151. }
  152. }