博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JS Array 进行无限制多条件排序 可以指定正反顺序
阅读量:2230 次
发布时间:2019-05-09

本文共 3318 字,大约阅读时间需要 11 分钟。

/************************************************************ * JSArrayOrder.js * 0.1 * JackieZheng  ************************************************************/function compare(a, b, orderArray, isReverse) {    let c = orderArray[0];    if (orderArray.length > 1 && (a[c] || a) === (b[c] || b)) {        return compare(a, b, orderArray.slice(1), isReverse);    } else {        return (a[c] || a) === (b[c] || b) ? 0 : ((isReverse ? (a[c] || a) > (b[c] || b) : (a[c] || a) < (b[c] || b)) ? -1 : 1);    }}/** * orderArray 指定排序属性,优先级依先后顺序 eg:["name", "num", "time"] * isReverse 是否倒序,默认 false */Array.prototype.orderBy = (Array.prototype.orderBy = function (orderArray, isReverse) {    if (typeof (orderArray) === 'boolean' || typeof (orderArray) === "undefined") {        isReverse = orderArray;        orderArray = '';    }    if (typeof (orderArray) === 'string') {        let str = orderArray;        orderArray = [];        orderArray.push(str);    }    return this.sort((a, b) => {        return compare(a, b, orderArray, isReverse)    })})
/*** 以下测试代码***//测试例子var arr = [{    name: 'b',    num: 2,    time: '2015-06-08 13:44:18'}, {    name: 'b',    num: 3,    time: '2015-06-08 13:44:18'}, {    name: 'a',    num: 4,    time: '2015-06-07 13:40:18'}, {    name: 'a',    num: 4,    time: '2015-06-08 13:44:18'}, {    name: 'c',    num: 6,    time: '2015-06-07 13:40:18'}, ];//单项默认排序arr.orderBy("num"); //结果(5) [{…}, {…}, {…}, {…}, {…}]0: {name: "b", num: 2, time: "2015-06-08 13:44:18"}1: {name: "b", num: 3, time: "2015-06-08 13:44:18"}2: {name: "a", num: 4, time: "2015-06-08 13:44:18"}3: {name: "a", num: 4, time: "2015-06-07 13:40:18"}4: {name: "c", num: 6, time: "2015-06-07 13:40:18"}length: 5__proto__: Array(0)//单项反向排序arr.orderBy("num",true);  //结果(5) [{…}, {…}, {…}, {…}, {…}]0: {name: "c", num: 6, time: "2015-06-07 13:40:18"}1: {name: "a", num: 4, time: "2015-06-08 13:44:18"}2: {name: "a", num: 4, time: "2015-06-07 13:40:18"}3: {name: "b", num: 3, time: "2015-06-08 13:44:18"}4: {name: "b", num: 2, time: "2015-06-08 13:44:18"}length: 5__proto__: Array(0) //多项默认排序arr.orderBy(["name", "num", "time"]); //结果(5) [{…}, {…}, {…}, {…}, {…}]0: {name: "a", num: 4, time: "2015-06-07 13:40:18"}1: {name: "a", num: 4, time: "2015-06-08 13:44:18"}2: {name: "b", num: 2, time: "2015-06-08 13:44:18"}3: {name: "b", num: 3, time: "2015-06-08 13:44:18"}4: {name: "c", num: 6, time: "2015-06-07 13:40:18"}length: 5__proto__: Array(0)//多项反向排序arr.orderBy(["name", "num", "time"], true);//结果(5) [{…}, {…}, {…}, {…}, {…}]0: {name: "c", num: 6, time: "2015-06-07 13:40:18"}1: {name: "b", num: 3, time: "2015-06-08 13:44:18"}2: {name: "b", num: 2, time: "2015-06-08 13:44:18"}3: {name: "a", num: 4, time: "2015-06-08 13:44:18"}4: {name: "a", num: 4, time: "2015-06-07 13:40:18"}length: 5__proto__: Array(0) //简单默认排序["b","c","a"].orderBy()//结果(3) ["a", "b", "c"]0: "a"1: "b"2: "c"length: 3__proto__: Array(0)//简单反向排序["b","c","a"].orderBy(true)//结果(3) ["c", "b", "a"]0: "c"1: "b"2: "a"length: 3__proto__: Array(0)//简单默认排序[11,2,1,6,5,10,56].orderBy()//结果(7) [1, 2, 5, 6, 10, 11, 56]0: 11: 22: 53: 64: 105: 116: 56length: 7__proto__: Array(0)//简单反向排序[11,2,1,6,5,10,56].orderBy(true)//结果(7) [56, 11, 10, 6, 5, 2, 1]0: 561: 112: 103: 64: 55: 26: 1length: 7__proto__: Array(0) *** 测试代码 结束***/

PS:有兴趣可以修改代码,比如实现为单独条件指定 顺序。

Github:

 

转载地址:http://oskbb.baihongyu.com/

你可能感兴趣的文章
深入理解JVM虚拟机10:JVM常用参数以及调优实践
查看>>
深入理解JVM虚拟机12:JVM性能管理神器VisualVM介绍与实战
查看>>
深入理解JVM虚拟机13:再谈四种引用及GC实践
查看>>
Spring源码剖析1:Spring概述
查看>>
Spring源码剖析2:初探Spring IOC核心流程
查看>>
Spring源码剖析5:JDK和cglib动态代理原理详解
查看>>
Spring源码剖析6:Spring AOP概述
查看>>
探索Redis设计与实现9:数据库redisDb与键过期删除策略
查看>>
探索Redis设计与实现10:Redis的事件驱动模型与命令执行过程
查看>>
分布式系统理论基础1: 一致性、2PC和3PC
查看>>
分布式系统理论基础2 :CAP
查看>>
分布式系统理论基础3: 时间、时钟和事件顺序
查看>>
分布式系统理论基础4:Paxos
查看>>
分布式系统理论基础5:选举、多数派和租约
查看>>
分布式系统理论基础6:Raft、Zab
查看>>
分布式系统理论进阶7:Paxos变种和优化
查看>>
分布式系统理论基础8:zookeeper分布式协调服务
查看>>
搞懂分布式技术1:分布式系统的一些基本概念
查看>>
搞懂分布式技术2:分布式一致性协议与Paxos,Raft算法
查看>>
搞懂分布式技术3:初探分布式协调服务zookeeper
查看>>