Sort Chinese characters by first letter

Source: Internet
Author: User

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

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.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.