JS Array 进行无限制多条件排序 可以指定正反顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /************************************************************ * 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) }) }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | /*** 以下测试代码*** //测试例子 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: 1 1: 2 2: 5 3: 6 4: 10 5: 11 6: 56 length: 7 __proto__: Array(0) //简单反向排序 [11,2,1,6,5,10,56].orderBy(true) //结果 (7) [56, 11, 10, 6, 5, 2, 1] 0: 56 1: 11 2: 10 3: 6 4: 5 5: 2 6: 1 length: 7 __proto__: Array(0) *** 测试代码 结束***/ |
PS:有兴趣可以修改代码,比如实现为单独条件指定 顺序。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构