About string sorting, sorting by numbers

Source: Internet
Author: User

Test.sort (Comparefunc);//Sort function custom sort, for irregular, number, string set//* Comparison function//* @param {Object} param1 parameter to compare 1//* @ param {Object} param2 the parameter to compare 2//* @return {number} if param1 > Param2 returns 1//* if param1 = par AM2 return 0//* if param1<param2returns -1//*/function Comparefunc (PARAM1,PARAM2) {//If the two parameters are string type if (typeof param1== "string" && typeof param2== "string")        {return param1.localecompare (param2); }//If parameter 1 is a number, parameter 2 is a string if (typeof param1== "number" && typeof param2== "string")        {return-1; }//If parameter 1 is a string, parameter 2 is a number if (typeof param1== "string" && typeof param2== "Number")        {return 1; }//If the two parameters are numeric if (typeof param1== "number" && typeof param2== "Number") {if (param1>param2) return 1;            if (param1 = = param2) return 0; if (param1<param2) return-1; }} var Testarray= ["Beijing", "Shanghai", "Dalian", "Chengdu");//Localecompare () method, sort by string, ascending, English, Chinese according to the first character Pinyin document.write (Testarray.sort (function comparefunctio  N (param1,param2) {return param1.localecompare (param2); Output: Beijing, Chengdu, Dalian, Shanghai});

About string sorting, sorting by numbers

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.