JS array sorting, support for reverse order and multi-dimension sorting

Source: Internet
Author: User
Tags array sort

Work encountered JS array sorting problem, the array is stored in the object, so Baidu, the use of other people's code to modify, the final completion can be reversed, reverse order, can be multi-dimensional sorting function
The source code is as follows:

/** * JS Array sort supports numbers and strings * @param params* @param arrobj obj Required Array Object * @param keyName string Required property name to sort * @param type int optional Default type:0 Zheng Shun type:1 */function arritemsort (arrobj,keyname,type) {//here if it is directly equal to arrobj, equivalent to just a reference to the object, changing the sort will affect the sorting of the original object at the same time.  by Arrobj.slice (0),//means the object is copied to another object, the two do not affect var temparrobj = arrobj.slice (0);          var compare = function (keyname,type) {return function (Obj1, obj2) {var val1 = Obj1[keyname];          var val2 = Obj2[keyname];              if (!isnan (number (VAL1)) &&!isnan (number (VAL2))) {val1 = number (VAL1);              Val2 = number (VAL2);          }//If the value is empty, put in the last if (val1 = = NULL && Val2 = = null) {return 0;          } else if (Val1 = = null && val2!= null) {return (type==1 -1:1);          } else if (Val2 = = null && val1!= null) {return (type==1 1:-1); }//Sort if(Val1 < Val2)          {return (type==1 1:-1);          } else if (Val1 > Val2) {return (type==1 -1:1);;          } else {return 0; }}} return Temparrobj.sort (compare (Keyname,type));}

Examples of Use:

var temp = [  {"name": "ZJF", "Score": "Age": ten},  {"name": "Lyy", "Score": All, "Age": 5},  {"name": "Zzx", " Score ": All," age ": 12}];//according to the age sort var temp1 = Arritemsort (temp," age ", 1); Console.log (TEMP1);//Sort by ages, then by fractions var Temp2 = Arritemsort (Arritemsort (temp, "age", 1), "Score", 1); Console.log (TEMP2);

Source: jsfun.cn

JS array sorting, support for reverse order and multi-dimension sorting

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.