Javascript 排序数组或对象
分享一个用于数组或者对象的排序的函数。该函数可以以任意深度的数组或者对象的值作为排序基数对数组或的元素进行排序。
代码如下:
1 /** 2 * 排序数组或者对象 3 * by Jinko 4 * date 2015-12-23 5 * @param object 数组或对象 6 * @param subkey 需要排序的子键, 该参数可以是字符串, 也可以是一个数组 7 * @param desc 排序方式, true:降序, false|undefined:升序 8 * @returns {*} 返回排序后的数组或者对象 9 * 10 * 注意: 对于对象的排序, 如果使用console.log打印对象的显示可能和排序结果不一致, 11 * 其键会被浏览器以字母顺序排序显示,但在for循环中则为正确的排序顺序 12 */ 13 function sort_object(object, subkey, desc) 14 { 15 var is_array = false; 16 17 if(Object.prototype.toString.call(object) === '[object Array]') { 18 is_array = true; 19 } 20 21 if(is_array) { 22 var keys = {length:object.length}; 23 } else { 24 if(typeof(Object.keys) == 'function') { 25 var keys = Object.keys(object); 26 } else{ 27 var keys = []; 28 for(var key in keys) { 29 keys.push(key); 30 } 31 } 32 } 33 34 for(var i=0; i<keys.length; i++) { 35 for(var j=i+1; j<keys.length; j++) { 36 37 if(is_array) { 38 //数组排序 39 if(Object.prototype.toString.call(subkey) === '[object Array]') { 40 var vali = object[i]; 41 var valj = object[j]; 42 43 for(var si=0; si<subkey.length; si++) { 44 vali = vali[ subkey[si] ]; 45 valj = valj[ subkey[si] ]; 46 } 47 } else { 48 if((!subkey && subkey !== 0) || subkey == '' && object.sort) { 49 var vali = object[i]; 50 var valj = object[j]; 51 } else { 52 var vali = object[i][subkey]; 53 var valj = object[j][subkey]; 54 } 55 } 56 57 if(desc) { 58 if(valj > vali) { 59 var tmp = object[i]; 60 object[i] = object[j]; 61 object[j] = tmp; 62 } 63 } else { 64 if(valj < vali) { 65 var tmp = object[i]; 66 object[i] = object[j]; 67 object[j] = tmp; 68 } 69 } 70 } else { 71 //对象排序 72 var obi = object[ keys[i] ]; 73 var obj = object[ keys[j] ]; 74 75 if(Object.prototype.toString.call(subkey) === '[object Array]') { 76 var vali = obi; 77 var valj = obj; 78 79 for(var si=0; si<subkey.length; si++) { 80 vali = vali[ subkey[si] ]; 81 valj = valj[ subkey[si] ]; 82 } 83 } else { 84 if((!subkey && subkey !== 0) || subkey == '' && object.sort) { 85 var vali = obi; 86 var valj = obj; 87 } else { 88 var vali = obi[subkey]; 89 var valj = obj[subkey]; 90 } 91 } 92 93 if(desc) { 94 if(valj > vali) { 95 var tmp = keys[i]; 96 keys[i] = keys[j]; 97 keys[j] = tmp; 98 } 99 } else { 100 if(valj < vali) { 101 var tmp = keys[i]; 102 keys[i] = keys[j]; 103 keys[j] = tmp; 104 } 105 } 106 }//is!array 107 } 108 } 109 110 if(is_array) { 111 return object; 112 } else { 113 var sorted = {}; 114 115 for(var i=0; i<keys.length; i++) { 116 sorted[ keys[i] ] = object[ keys[i] ]; 117 } 118 119 return sorted; 120 } 121 } //sort_object
用法如下:
用法1:
1 var data = { 2 "a": { 3 "session_offline": 21, 4 "session_online": 6, 5 "session_count": 1 6 }, 7 "b": { 8 "session_offline": 15, 9 "session_online": 5, 10 "session_count": 1 11 }, 12 "c": { 13 "session_offline": 6, 14 "session_online": 1, 15 "session_count": 1 16 }, 17 "d": { 18 "session_offline": 2, 19 "session_online": 0, 20 "session_count": 1 21 } 22 }; 23 24 //根据session_online字段升序排序 25 data = sort_object(data, 'session_online'); 26 27 for(var k in data) { 28 console.log(data[k]); 29 } 30 31 console.log('------------------'); 32 33 //根据session_offline字段降序排序 34 data = sort_object(data, 'session_offline', true); 35 36 for(var k in data) { 37 console.log(data[k]); 38 }
用法2:
1 var data2 = [ 2 { 3 "cpu": 24, 4 "cpuhz": 2099, 5 "cpuhz_use": 1322, 6 }, 7 { 8 "cpu": 24, 9 "cpuhz": 2099, 10 "cpuhz_use": 694, 11 }, 12 { 13 "cpu": 24, 14 "cpuhz": 2099, 15 "cpuhz_use": 1622, 16 }, 17 { 18 "cpu": 24, 19 "cpuhz": 2099, 20 "cpuhz_use": 322, 21 } 22 ]; 23 24 //根据cpuhz_use字段进行排序 25 data2 = sort_object(data2, 'cpuhz_use'); 26 27 console.log(data2);
用法3:
1 var data3 = [1,3,2,5,9,7,3,7]; 2 3 //对一维数组进行升序排序 4 data3 = sort_object(data3); 5 console.log(data3); 6 7 //对一维数组进行降序排序 8 data3 = sort_object(data3, null, true); 9 console.log(data3);
用法4:
1 var data4 = {'a':45, 'b':36, 'c':11, 'd':32}; 2 3 //对对象进行升序排序 4 data4 = sort_object(data4); 5 //对对象进行降序排序 6 data4 = sort_object(data4, '', true); 7 8 for(var k in data4) { 9 console.log(k, ':', data4[k]); 10 }
用法5:
1 var data5 = { 2 "l1_1": { 3 "l2": { 4 "l3": 1 5 } 6 }, 7 "l1_2": { 8 "l2": { 9 "l3": 3 10 } 11 }, 12 "l1_3": { 13 "l2": { 14 "l3": 2 15 } 16 }, 17 "l1_4": { 18 "l2": { 19 "l3": 4 20 } 21 } 22 }; 23 24 //对对象元素的l2下的l3的值为基础进行升序排序 25 data5 = sort_object(data5, ['l2', 'l3']); 26 27 for(var k in data5) { 28 console.log(data5[k].l2); 29 }
用法6:
1 var data6 = [ 2 [ 3 { 4 "a": 1 5 }, 6 2, 7 3 8 ], 9 [ 10 { 11 "a": 6 12 }, 13 4, 14 7 15 ], 16 [ 17 { 18 "a": 0 19 }, 20 1, 21 6 22 ] 23 ]; 24 25 //对数组的元素以下标为0的元素的键名为a的值为基础进行升序排序 26 data6 = sort_object(data6, [0,'a']); 27 28 for(var k = 0; k<data6.length; k++) { 29 console.log(data6[k]); 30 } 31 32 console.log('---------------------'); 33 34 //对数组的元素以下标为2的元素的值为基础进行升序排序 35 data6 = sort_object(data6, [2]); // 等价于 data6 = sort_object(data6, 2); 36 37 for(var k = 0; k<data6.length; k++) { 38 console.log(data6[k]); 39 }