The project needs a choice of city name function, the specific data can be found on the website of the National Bureau of Statistics, there are many direct search, such as http://blog.jjonline.cn/phptech/172.html.
The data is there, but how are Chinese characters sorted alphabetically?
First, the front-end JS processing
var a = ["Shanghai", "Dalian", "Beijing", "Lhasa", "Tianjin"// return ["Beijing", "Dalian", "Lhasa", "Shanghai", "Tianjin"]A.sort ( function(A, A, (a) {return A.localecompare (b)});
Localecompare Definition and usage
Compares two strings in a local-specific order.
Grammar
Stringobject.localecompare (target)
Parameters |
Description |
Target |
The string to compare with Stringobject in a local-specific order. |
return value
A number that describes the result of the comparison. If Stringobject is less than target, Localecompare () returns a number less than 0. If Stringobject is greater than target, the method returns a number greater than 0. If two strings are equal, or if there is no difference based on the local collation, the method returns 0.
So, the processing of the actual JSON object array is:
var a = [ ' A ', phone:1, Value: ' Shanghai '}, ' B ', phone:5, Value: ' Dalian '}, ' d ', p Hone:3, Value: "Beijing"}, ' C ', Phone:4, Value: "Lhasa"}]; A.sort (A,b) { return
Second, the background of PHP processing
foreach($array as $key=$value) { $new _array[$key] =Iconv(' UTF-8 ', ' GBK ',$value); } Asort($new _array); foreach($new _array as $key=$value) { $array[$key] =Iconv(' GBK ', ' UTF-8 ',$value); }
Sort Chinese characters by first letter