Javascript OrderBy
要在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];//處理valuevar a = v1, b = v2;var sortKey = condition.sortKey;if (sortKey !== undefined) {a = v1[sortKey];b = v2[sortKey];}//區分 valueTypevar 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 {//打平手的話梯歸比下一個,當有多個orderByconditionIndex++;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在前就不要轉 -1return -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的)?
轉載于:https://www.cnblogs.com/keatkeat/p/3896493.html
總結
以上是生活随笔為你收集整理的Javascript OrderBy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在asp.net中使用加密数据库联接字符
- 下一篇: Jersey 入门与Javabean