今天查到数组的sort函数可以接受自定义比较函数,这真是一件妙事儿。
这样对(数字类型表示范围以内的)数字进行排序就变得极为简单了。
- var myArray = [-80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,7.999,20,0,22,55,312];
- iJs.put(myArray.sort(function(a,b){return a-b}));
调试信息:
-80,-13.888,-10,-9.6,-9.4,-1,0,2,2,3,5.823,7.999,20,22,55,312
如果想降序,a-b改为b-a即可:
- var myArray = [-80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,0,7.999,20,22,55,312];
- iJs.put(myArray.sort(function(a,b){return b-a}));//此处为b-a
调试信息:
312,55,22,20,7.999,5.823,3,2,2,0,-1,-9.4,-9.6,-10,-13.888,-80
312,55,22,20,7.999,5.823,3,2,2,0,-1,-9.4,-9.6,-10,-13.888,-80
构造返回比较函数的函数,可以达到更为灵活的目的。为此我写了一个一维二维数组通用的排序函数如下(未经充分测试,如用于生产,最好再测一下啊):
- //类型相同则比大小,升降由option的第一个参数指定
- //类型不同则比类型,升降由option的第二个参数指定
- //如果是对象或数组,则按name指定属性或元素参与排序
- //比大小结果相同则按照secondby指定的规则排序
- var by = function(name,option,secondby){
- return function(o,p){
- var a,b;
- var sign=1;//调节顺序
- var r;//保存返回值
- //如果是对象或数组,则按name指定属性或元素参与排序
- if(o && typeof o === 'object'){
- a = o[name];
- }else{
- a = o;
- }
- if(p && typeof p === 'object'){
- b = p[name];
- }else{
- b = p;
- }
- if(typeof a === typeof b){
- //升降由option的第一个参数指定
- switch(option[0]){
- case "desc" :
- sign = 1;
- break;
- case "asc" :
- sign = -1;
- break;
- default:
- sign = 1;//默认
- }
- }else{
- //类型不同则比类型
- a = typeof a;
- b = typeof b;
- //升降由option的第二个参数指定
- switch(option[1]){
- case "desc" :
- sign = 1;
- break;
- case "asc" :
- sign = -1;
- break;
- default:
- sign = 1;//默认
- }
- }
- if(a === b){
- //递归支持多个参数排序(20121231补充:如果传进来不是by则不是递归)
- r = typeof secondby === "function" ? secondby(o,p):0;
- }else if(a<b){
- r = 1*sign;
- }else{
- r = -1*sign;
- }
- return r;
- }
- }
- //判断是否数组
- var is_array = function(value){
- return value && //有值
- typeof value === "object" && //typeof查出类型为object
- typeof value.length === "number" && //length属性是number类型
- typeof value.splice === "function" && //有splice方法
- !(value.propertyIsEnumerable("length"))//length是一个不可枚举的属性
- }
- //输出二维数组
- var puts = function(myObj){
- for(var i=0;i<myObj.length;i+=1){
- var tmp = "";
- if(is_array(myObj[i])){
- for(var j=0;j<myObj[i].length;j+=1){
- tmp = tmp + myObj[i][j] + ",";
- }
- }else{
- for(name in myObj[i]){
- tmp = tmp + name + ":" + myObj[i][name] + ",";
- }
- }
- iJs.put(tmp);
- }
- }
- var myArray=new Array();
- //例1
- myArray = [
- {"a":1,"b":1,"c":1},
- {"a":3,"b":3,"c":1},
- {"a":3,"b":4,"c":1},
- {"a":3,"b":1,"c":1},
- {"a":2,"b":1,"c":2},
- {"a":2,"b":1,"c":1},
- ];
- myArray = myArray.sort(
- by("a",["asc"],
- by("b",[],
- by("c",["asc"])))
- );
- puts(myArray);
- //例2
- myArray = [
- [1,1,1],
- [3,3,1],
- [3,4,1],
- [3,1,1],
- [2,1,2],
- [2,1,1]
- ];
- myArray = myArray.sort(
- by(0,["asc"],
- by(1,[],
- by(2,["asc"])))
- );
- puts(myArray);
- //例3
- myArray = [-80,-13.888,-10,-9.6,-9.4,-1,2,2,3,5.823,0,7.999,20,22,55,312];
- myArray = myArray.sort(
- by(null,["asc"])
- );
- iJs.put("");
- iJs.put(myArray);
调试信息:
a:1,b:1,c:1,
a:2,b:1,c:1,
a:2,b:1,c:2,
a:3,b:4,c:1,
a:3,b:3,c:1,
a:3,b:1,c:1,
1,1,1,
2,1,1,
2,1,2,
3,4,1,
3,3,1,
3,1,1,
-80,-13.888,-10,-9.6,-9.4,-1,0,2,2,3,5.823,7.999,20,22,55,312
a:1,b:1,c:1,
a:2,b:1,c:1,
a:2,b:1,c:2,
a:3,b:4,c:1,
a:3,b:3,c:1,
a:3,b:1,c:1,
1,1,1,
2,1,1,
2,1,2,
3,4,1,
3,3,1,
3,1,1,
-80,-13.888,-10,-9.6,-9.4,-1,0,2,2,3,5.823,7.999,20,22,55,312
想获得全部可执行代码,请下载附件。
附件:http://down.51cto.com/data/2362064
本文转自 hexiaini235 51CTO博客,原文链接:http://blog.51cto.com/idata/1104810,如需转载请自行联系原作者