Javascript OrderBy
2024-10-18 09:55:17
要在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的)
最新文章
- [WP8.1开发]RSA 使用BouncyCastle 公钥解密
- 数据库如何生成sql语句
- 关于成为Java高级工程师之路
- 在Windows 8.1及IE 11中如何使用HttpWatch
- [.NET领域驱动设计实战系列]专题四:前期准备之工作单元模式(Unit Of Work)
- SSL、OPENSSL、SSH、OPENSSH
- java 多线程—— 线程等待与唤醒
- 自学QT笔记
- lighttpd的超时参数详解
- [MySQL] MySQL的自己主动化安装部署
- SSIS CDC(Change Data Capture)组件在数据库中启用报错。 The error returned was 14234: &#39;The specified &#39;@server&#39; is invalid
- c#异步和多线程有什么区别和联系?
- AE + GDAL实现影像按标准图幅分割(下)
- RabbitMQ-客户端
- Mysql之表的操作与索引操作
- CSDN_投票评选_JS_分析脚本
- Python爬虫-尝试使用人工和OCR处理验证码模拟登入
- shell实战之Linux主机系统监控
- H5公共样式,用于所有H5开发页面
- 剑指offer八之跳台阶
热门文章
- java笔记15之this
- 史上最全maven pom.xml详解
- 关于Hibernate脏检查与快照
- 装多版本号sqlserver的远程连接问题
- hdu 1685 Booksort (IDA*)
- [Angular 2] Passing Observables into Components with Async Pipe
- 使用__doPostBack函数来达到使用客户端的控件来调用服务器端的函数的--小结
- WebStorm修改默认快捷键
- sql server 导出
- mysql 存储引擎MYSIAM和INNODB特性比较