JavaScript itself implements a mixed sorting method of numbers \ Letters and Chinese by Fungleo

Source: Internet
Author: User
Tags javascript array

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
    1. If it's a number, it's directly a number.
    2. If it is a string, it is charCodeAt() sorted using conversion Unicode to encoding.
    3. Unicodeis an integer between 0-65535
Other Notes
    1. 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 .
    2. 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.
    3. 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
    4. 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

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.