Home > Article > Web Front-end > JavaScript object array sorting function and six uses_javascript skills
Share a function for sorting arrays or objects. This function can sort the elements of an array or object using the value of an array or object of any depth as the sorting base.
The code is as follows:
/** * 排序数组或者对象 * by Jinko * date -- * @param object 数组或对象 * @param subkey 需要排序的子键, 该参数可以是字符串, 也可以是一个数组 * @param desc 排序方式, true:降序, false|undefined:升序 * @returns {*} 返回排序后的数组或者对象 * * 注意: 对于对象的排序, 如果使用console.log打印对象的显示可能和排序结果不一致, * 其键会被浏览器以字母顺序排序显示,但在for循环中则为正确的排序顺序 */ function sort_object(object, subkey, desc) { var is_array = false; if(Object.prototype.toString.call(object) === '[object Array]') { is_array = true; } if(is_array) { var keys = {length:object.length}; } else { if(typeof(Object.keys) == 'function') { var keys = Object.keys(object); } else{ var keys = []; for(var key in keys) { keys.push(key); } } } for(var i=; i<keys.length; i++) { for(var j=i+; j<keys.length; j++) { if(is_array) { //数组排序 if(Object.prototype.toString.call(subkey) === '[object Array]') { var vali = object[i]; var valj = object[j]; for(var si=; si<subkey.length; si++) { vali = vali[ subkey[si] ]; valj = valj[ subkey[si] ]; } } else { if((!subkey && subkey !== ) || subkey == '' && object.sort) { var vali = object[i]; var valj = object[j]; } else { var vali = object[i][subkey]; var valj = object[j][subkey]; } } if(desc) { if(valj > vali) { var tmp = object[i]; object[i] = object[j]; object[j] = tmp; } } else { if(valj < vali) { var tmp = object[i]; object[i] = object[j]; object[j] = tmp; } } } else { //对象排序 var obi = object[ keys[i] ]; var obj = object[ keys[j] ]; if(Object.prototype.toString.call(subkey) === '[object Array]') { var vali = obi; var valj = obj; for(var si=; si<subkey.length; si++) { vali = vali[ subkey[si] ]; valj = valj[ subkey[si] ]; } } else { if((!subkey && subkey !== ) || subkey == '' && object.sort) { var vali = obi; var valj = obj; } else { var vali = obi[subkey]; var valj = obj[subkey]; } } if(desc) { if(valj > vali) { var tmp = keys[i]; keys[i] = keys[j]; keys[j] = tmp; } } else { if(valj < vali) { var tmp = keys[i]; keys[i] = keys[j]; keys[j] = tmp; } } }//is!array } } if(is_array) { return object; } else { var sorted = {}; for(var i=; i<keys.length; i++) { sorted[ keys[i] ] = object[ keys[i] ]; } return sorted; } } //sort_object
is used as follows:
Usage 1:
var data = { "a": { "session_offline": , "session_online": , "session_count": }, "b": { "session_offline": , "session_online": , "session_count": }, "c": { "session_offline": , "session_online": , "session_count": }, "d": { "session_offline": , "session_online": , "session_count": } }; //根据session_online字段升序排序 data = sort_object(data, 'session_online'); for(var k in data) { console.log(data[k]); } console.log('------------------'); //根据session_offline字段降序排序 data = sort_object(data, 'session_offline', true); for(var k in data) { console.log(data[k]); }
Usage 2:
var data = [ { "cpu": , "cpuhz": , "cpuhz_use": , }, { "cpu": , "cpuhz": , "cpuhz_use": , }, { "cpu": , "cpuhz": , "cpuhz_use": , }, { "cpu": , "cpuhz": , "cpuhz_use": , } ]; //根据cpuhz_use字段进行排序 data = sort_object(data, 'cpuhz_use'); console.log(data);
Usage 3:
var data = [,,,,,,,]; //对一维数组进行升序排序 data = sort_object(data); console.log(data); //对一维数组进行降序排序 data = sort_object(data, null, true); console.log(data);
Usage 4:
var data = {'a':, 'b':, 'c':, 'd':}; //对对象进行升序排序 data = sort_object(data); //对对象进行降序排序 data = sort_object(data, '', true); for(var k in data) { console.log(k, ':', data[k]); }
Usage 5:
var data = { "l_": { "l": { "l": } }, "l_": { "l": { "l": } }, "l_": { "l": { "l": } }, "l_": { "l": { "l": } } }; //对对象元素的l下的l的值为基础进行升序排序 data = sort_object(data, ['l', 'l']); for(var k in data) { console.log(data[k].l); }
Usage 6:
var data = [ [ { "a": }, , ], [ { "a": }, , ], [ { "a": }, , ] ]; //对数组的元素以下标为的元素的键名为a的值为基础进行升序排序 data = sort_object(data, [,'a']); for(var k = ; k<data.length; k++) { console.log(data[k]); } console.log('---------------------'); //对数组的元素以下标为的元素的值为基础进行升序排序 data = sort_object(data, []); // 等价于 data = sort_object(data, ); for(var k = ; k<data.length; k++) { console.log(data[k]); }
The above content is the entire description of the JavaScript object array sorting function and six usages shared by the editor. I hope you like it.