JavaScript's own approach to mixed sorting of numbers \ Letters and Chinese (purely research, not practical) preface
In the previous blog post, "the sort method of JavaScript array sorting and the learning summary of self-implementing sorting methods", I implemented the sorting of digital arrays in my own way.
Of course, in practical use, I will still use the sort
method more convenient. However, my last blog post, only the implementation of the digital sort, but srot
the method by default can be sorted by the alphabet Oh! And my code can only sort numbers, and it looks weak.
So, I have to add a sorting method that can arrange letters or even Chinese.
Implementation code
$( function(){ vararr = ["Jack","book","Fung", the,"Love","Mark","China","China","Phone","Andy Lau"]; Console.log (' original array '); Console.log (arr); Console.log (' for method from small to large sort '); Console.log (Arrsortmintomax (arr)); Console.log (' for method from large to small sort '); Console.log (Arrsortmaxtomin (arr)); Console.log (' original array '); Console.log (arr);}); function arrminnum(arr){ varMinnum =Infinity, index =-1, Minvul =""; for(vari =0; i < arr.length; i++) {if(typeof(Arr[i]) = ="string") {if(Arr[i].charcodeat () <minnum) {minnum = Arr[i].charcodeat (); Minvul = Arr[i]; index = i; } }Else{if(Arr[i]<minnum) {minnum = Arr[i]; Minvul = arr[i] index = i; } } };return{"Minnum": Minvul,"Index": index};} function arrsortmintomax(arr){ varArrnew = [];varArrold = Arr.concat (); for(vari =0; i < arr.length; i++) {Arrnew.push (Arrminnum (arrold). Minnum); Arrold.splice (Arrminnum (arrold). Index,1) };return(arrnew);} function arrmaxnum(arr){ varMaxnum =-Infinity, index =-1, Maxvul =""; for(vari =0; i < arr.length; i++) {if(typeof(Arr[i]) = ="string") {if(Arr[i].charcodeat () >maxnum) {maxnum = Arr[i].charcodeat (); Maxvul = Arr[i]; index = i; } }Else{if(Arr[i]>maxnum) {maxnum = Arr[i]; Maxvul = Arr[i]; index = i; } } };return{"Maxnum": Maxvul,"Index": index};} function arrsortmaxtomin(arr){ varArrnew = [];varArrold = Arr.slice (0); for(vari =0; i < arr.length; i++) {Arrnew.push (Arrmaxnum (arrold). Maxnum); Arrold.splice (Arrmaxnum (arrold). Index,1); };return(arrnew);}
Run as follows:
Sorting principle
- If it's a number, it's directly a number.
- If it is a string, it is
charCodeAt()
sorted using conversion Unicode
to encoding.
Unicode
is an integer between 0-65535
Other Notes
- According to the normal sorting logic, it should be: The numbers are smaller than all the letters, the letters are smaller than all Chinese, the Chinese should be sorted according to the first letter of the first word pinyin .
- I have this code in addition to the letter is smaller than all Chinese is the implementation of this one, the other has not been implemented.
- Logic should also be able to achieve, the number of letters to find out the Chinese, numbers and arrays to compare, letters and letters compared, Chinese and Chinese comparison, and then splicing array
- Getting the first letter of the first word may be a little more troublesome.
Chinese characters can actually be directly compared to the right.
As shown, it makes sense for Zhang Fei to be the boss. javascript
Finally after thousand years for Zhang Fei's rectification, when he should be the boss of the!~
This article is original by Fungleo, allow reprint. But reprint must be signed by the author, and keep the article starting link. Otherwise, legal liability will be investigated.
Starting Address: http://blog.csdn.net/FungLeo/article/details/51583344
JavaScript itself implements a mixed sorting method of numbers \ Letters and Chinese by Fungleo