In general, people will use the following method for Chinese characters pinyin sort
var list=[' king ', 'a', 'li'];
list.Sort(function (a, b) {return a.localeCompare(b); });
localeCompare () : with local specific order to compare two strings.
This approach to pinyin sort is unreliable.
Second way: very dependent on Chinese operating system
Is very dependent on the browser kernel that is to say, if your site visitors are through the Chinese system, or the Internet explorer browser (Chrome), then he will probably unable to see the pinyin sort the result we expected.
Here I'll introduce my solution to this problem, hope to be able to derive somehow:
this method supports the Unicode character set x4e00 from 0 to 0 x9fa5 area a total of 20902 consecutive from China (including Taiwan), Japan, South Korea, Chinese characters, namely, CJK (Chinese Japanese Korean) characters.
var CompareStrings={.........}
getOrderedUnicode: function (char) {
var originalUnicode=char.charCodeAt ();
if (originalUnicode >=0 x4e00 && originalUnicode <=0 x9fa5) {
var index=this.Db.IndexOf (char);
if (index >1) {
return index + 0 x4e00;
}}
return originalUnicode;
},
compare: function (a, b) {
if (a==b) {return 0; }
//here can be rewritten according to the specific needs and the writing is the empty string at the bottom the 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 [i] (b);
if (au >bu) {
return 1;
} else if (au <bu) {
return - 1;
}}
return a.length >B.length? 1:1;
}}
//rewriting system native localeCompare
The prototype:
LocaleCompare = function (param) {
return CompareStrings.compare said (enclosing the toString (), param);
}
you can through the links below to download the complete code
A brief introduction of the principle of implementation:
According to pinyin sort good character (db) : there are multiple ways to achieve a goal, I am done with JavaScript + c# combination, use the script first put all the enumeration of Chinese characters, and then submitted to the c #good background sort, and output to the front desk, this is just the preparation, what all can.
Identify two characters who is bigger (getOrderedUnicode) : because when ordering, not only to deal with Chinese characters, and Chinese characters outside of the characters, so the comparator must be able to identify all of the characters, we here by judging whether a character is to discriminate Chinese characters: if it is Chinese characters, then the sort good word library search index, the index value plus the Unicode character set the location of the first Chinese characters, is after the "calibration" of the Unicode character set of the index value; If not Chinese characters, then return it directly on the index value of the Unicode character set.
Compare two strings (compare) : by comparing two each of the characters (within the effective range comparison, that is, the shorter the length of the string), if you find a greater than b, it returns 1, vice return 1.
Within the effective range after the comparison if haven't the tie, just see who is longer, such as a='123', b='1234', so long b to row in the back.
EDIT
You can also use JQuery plugin:
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"chinese-string-asc" : function (s1, s2) {
return s1.localeCompare(s2);
},
"chinese-string-desc" : function (s1, s2) {
return s2.localeCompare(s1);
}
} );
See the original post.