码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript OrderBy

时间:2014-08-07 12:26:39      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   java   io   for   art   

要在js 实现orderBy
基本知识就是 array.sort
array.sort(function(a,b){
 a 表示 row 0
 b 表示 row 1 
它会loop多次
你可以比较 if(a > b) return 1 
做出一个return , return 的结果 >0 代表你要这2个row对换位置 
})

要实现orderBy呢
逻辑就是在比较的时候如果出现一样值,你要拿接下来的column做对比,直到完结!是一种梯归手法 
if(a > b) return 1
else (a < b) return -1 
else
{
平手 
var a = row1["color"] //换下一个column的值对比
var b = row2["color"]
  if(a > b) return 1 
else if ..
else ...
又平手再循环...
}
sort 是游览器自带的功能。它的sort法法我不知道,排序本来就有很多很多种方法。(但可以支持上面的思想!这样很不错)。
一般我们对orderBy的思想可能是先sort 第一次,然后把相同的分层多个part ,在每个层sort 一次,一直这样循环 。 这其实是错误的。
在第一次loop时就要把逻辑给进去。一次sort 完成就可以了。

下面上一段代码,基本上可以用,只是要修改一些基本函数的依赖。

    //orderBy array是引用哦
    Array.prototype.orderBy = function (conditionList) {
        conditionList = facade_(conditionList);
        //一个外观处理
        //上层调用:
        //array.orderBy()顺序(not support value object|array)
        //array.orderBy("-")逆序(not support value object|array)
        //array.orderBy("code,-color,size") - 代表desc code代表attr (not support 有2中valueType的)
        //array.orderBy("0,-1,2") 0代表 array index (not support 有2中valueType的)
        function facade_(para) {
            //如果para 完全没有
            if (para === undefined) {
                return [{}]; //顺序,只能处理 value not array|object
            }
            else if (G.isString(para)) {
                var split = para.split(",");
                var conditionList = [];
                split.forEach(function (value) {
                    var condition = {};
                    var firstChar = value.charAt(0);
                    if (firstChar === "-") {
                        condition.numberSort = "desc";
                        value = value.substring(1);
                    }
                    if (value != "") {
                        if (G.canBeNumber(value)) {
                            value = +value;
                        }
                        condition.sortKey = value; //试图转去number
                    }
                    conditionList.push(condition);
                });
                return conditionList;
            }
        }
        //API : 调用
        //var result = array.orderBy([{ sortKey, numberFirst, numberSort, otherSort }]) 只区分是number or not number
        //sortKey : attr 或者 array的i , 没有代表value不是array|object 
        //numberFirst : true|false, default 是true , 当value有不同类型,时候number排在前面
        //numberSort : "desc"|"" default 是 "" 如果要desc,这是必填!
        //otherSort : "desc"|"" default 是 "" , 如果只有一种类型,那么就只用numberSort就可以了
        //逻辑规则,length = 0 error
        //value 是对象或array就一定有 conditionList[0].sortKey
        //value 是对象或array, 结构一定要一样,比如长短或attr                
        //精华 : 
        //array.sort(function(a,b){}) 
        //a,b 代表row 
        //return > 0 代表要转     
        var loop = function (v1, v2, conditionList, conditionIndex) {
            var result;
            var condition = conditionList[conditionIndex];
            //处理value
            var a = v1, b = v2;
            var sortKey = condition.sortKey;
            if (sortKey !== undefined) {
                a = v1[sortKey];
                b = v2[sortKey];
            }
            //区分 valueType
            var typeA = G.s.fn.myTypeOf(a);
            var typeB = G.s.fn.myTypeOf(b);
            if (typeA === typeB) {
                result = (condition.numberSort === undefined || condition.numberSort !== "desc") ? 1 : -1; //这个是给number的
                if (typeA !== "number" && condition.otherSort !== undefined) {
                    result = (condition.otherSort !== "desc") ? 1 : -1;
                }
                if (a > b) {
                    return result; //return 1 代表转
                }
                else if (a < b) {
                    return -result;
                }
                else {
                    //打平手的话梯归比下一个,当有多个orderBy
                    conditionIndex++;
                    if (conditionList[conditionIndex] !== undefined) {
                        return loop(v1, v2, conditionList, conditionIndex); //梯归
                    }
                    else {
                        return 0;
                    }
                }
            }
            else {
                //类型不同不能比,就看number要不要去前面就好
                result = (condition.is_numberFirst === undefined || condition.is_numberFirst === true) ? -1 : 1;
                if (typeA === "number") return result; //a 是number , 如果你要number在前就不要转 -1
                return -result;
            }
        };
        this.sort(function (v1, v2) {
            return loop(v1, v2, conditionList, 0);
        });
        return this;
    };

简单的可以这样调用 :

array.orderBy()顺序(not support value object|array)
array.orderBy("-")逆序(not support value object|array)
array.orderBy("code,-color,size") - 代表desc code代表attr (not support 有2中valueType的)
array.orderBy("0,-1,2") 0代表 array index (not support 有2中valueType的)

 

Javascript OrderBy,布布扣,bubuko.com

Javascript OrderBy

标签:des   style   blog   color   java   io   for   art   

原文地址:http://www.cnblogs.com/keatkeat/p/3896493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!