JavaScript to implement phonetic sorting method _javascript skills

Source: Internet
Author: User
In general, you will use the following method to do the phonetic sorting of Chinese characters
Copy Code code as follows:

var list = [' King ', ' Zhang ', ' Lee '];
List.sort (function (A, b) {
return A.localecompare (b);
});

Localecompare (): Compares two strings in a local-specific order.
The localecompare of this method for phonetic sorting is not to be relied on:
1. Very dependent on Chinese operating system
2. Very dependent on the browser's kernel
That is, if your site visitor is using a non-Chinese system, or a non-ie browser (such as Chrome), then he will probably not be able to see the phonetic sort results we expect.
--------------------------------------------------------------------------------
Here is a description of my solution to this problem, I hope to be able to introduce Kazakhstan:
This method supports a total of 20,902 Chinese characters from 0x4e00 to 0x9fa5 in the Unicode character set from China (including Taiwan), Japan and Korea, i.e. CJK (Chinese Japanese Korean).
Copy Code code as follows:

var comparestrings = {
DB: ' Acridine Ah Tong actinium ... 袰 襨 鐢 閪 闏 霻 ',//which omitted tens of thousands of words
Getorderedunicode:function (char) {
var originalunicode = Char.charcodeat ();
if (Originalunicode >= 0x4e00 && originalunicode <= 0x9fa5) {
var index = this.db.indexOf (char);
if (Index >-1) {
return index + 0x4e00;
}
}
return originalunicode;
},
Compare:function (A, b) {
if (a = = b) {return 0;
}
This can be rewritten according to specific requirements, and the current writing is to put the empty string in the last if (a.length = = 0) {return 1;}
if (B.length = = 0) {return-1}
var count = a.length > b.length? B.length:a.length;
for (var i = 0; i < count; i++) {
var au = this.getorderedunicode (a[i]);
var bu = this.getorderedunicode (b[i]);
if (au > Bu) {
return 1;
else if (au < bu) {
return-1;
}
}
return a.length > B.length? 1:-1;
}
}
Rewriting system native Localecompare
String.prototype.localeCompare = function (param) {
Return Comparestrings.compare (this.tostring (), param);
}

You can download the complete code via the link below Http://xiazai.jb51.net/201211/yuanma/js_pinyin_jb51.rar
Briefly introduce the principle of implementation:
1. Get alphabetical order of the Font (DB): There are a variety of ways to achieve the purpose, I was completed with the javascript+c# combination, the first script to all the Chinese characters enumerated, and then submitted to C # backstage sorting good, and then output to the front desk, this is just preparation work, how to do all can.
2. Determine the two characters who are larger (Getorderedunicode): Because the time of the sorting, not only to deal with Chinese characters, but also to deal with character other than Chinese characters, so the comparator must be able to recognize all the characters, here we are judged by whether a character is Chinese characters to treat differently: Then search for its index value in the sorted font, the resulting index value, plus the position of the first character in the Unicode character set, is the index value in the Unicode character set after calibration, and if it is not Chinese, returns its index value directly in the Unicode character set.
3. Compare two strings (Compare): compare each character in two strings (in valid range, that is, the length of the shorter string), and if you find a greater than B, return 1 and vice versa-1.
4. In the effectiveness of the comparison after the end of the game if not yet, to see who is relatively long, such as a= ' 123 ', b= ' 1234 ', then the longer B to row in the back.
Green channel: Good text to focus on my collection of the article contact me
Related Article

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.