Localecompare () method to achieve phonetic sorting in Chinese

Source: Internet
Author: User

Google has been a lot of time to find out on the foreign site on how to compare chinese, the original address: http://www.webdeveloper.com/forum/showthread.php?t=9365

Premise: Use Unicode encoding, If other GBK this method should not!

Definition and usage

Compares two strings in a local-specific order.

Stringobject.localecompare (target)

Target: a string to compare with Stringobject in a local-specific order

Test 1:

var str= "beijing"; var var2= "beijing"; var res=str.localecompare (var2); Console.log (res); Output 0

Test 2

var str= "Monday"; var var2= "Tuesday"; var res=str.localecompare (var2); Console.log (res); Output 1

Test 3

var str= "Wednesday"; var var2= "Monday"; var res=str.localecompare (var2); alert (res); Output-1

Test 4

To implement phonetic sorting in chinese:

<! DOCTYPE html>

  

Localecompare () method to achieve phonetic sorting in Chinese

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: info-contact@alibabacloud.com 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.