欢迎来到天天文库
浏览记录
ID:41395000
大小:63.66 KB
页数:7页
时间:2019-08-24
《JavaScript对象数组排序函数及六个用法_javascript技巧》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、JavaScript对象数组排序函数及六个用法分享一个用于数组或者对象的排序的函数。该函数可以以任意深度的数组或者对彖的值作为排序基数对数组或的元素进行排序。代码如下:/***排序数组或者对象*byJinko*date--*©paramobject数组或对彖*@paramsubkey需要排序的子键,该参数可以是字符串,也可以是一个数组*@paramdesc排序方式,true:降序,false]undefined:升序*©returns{*}返回排序后的数组或者对象**注意:对于对彖的排序,如果使用console,log打印对彖的显
2、示可能和排序结果不一致,*其键会被浏览器以字母顺序排序显示,但在for循环中则为正确的排序顺序*/functionsortobject(object,subkey,desc){"varis_array二false;if(Object,prototype.toString.call(objeet)二二二[objectArray]J){is_array=true;}"if(is_array){varkeys={length:object,length};}else{if(typeof(Object,keys)=='function")
3、{varkeys=Object,keys(object);}else{varkeys二[];for(varkeyinkeys){keys.push(key);}}}for(vari二;i4、;si5、6、subkev==&&object・sort){varvali二object[i];varvalj二object[j];}else{varvali=object[i][subkey];varvalj=object[j][subkey];}}if(desc){if(valj>vali){vartmp=object[i];object[i]7、=object[j];object[j]二tmp;}}else{if(valj8、;si++){vali=vali[subkey[si]];valj二valj[subkey[si]];}else{if((!subkey&&subkey!二二subkey二二''&&object,sort)varvali二obi;varvalj二obj;}else{varvali=obi[subkey];varvalj二obj[subkey];if(desc){if(valj>vartmpkeys[i]keys[j]}}else{if(valj9、;=tmp;vali){二keys[i];=keys[j];=tmp;}//is!arrayif(is_array){returnobject;}else{varsorted二{};for(vari=;i10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
4、;si5、6、subkev==&&object・sort){varvali二object[i];varvalj二object[j];}else{varvali=object[i][subkey];varvalj=object[j][subkey];}}if(desc){if(valj>vali){vartmp=object[i];object[i]7、=object[j];object[j]二tmp;}}else{if(valj8、;si++){vali=vali[subkey[si]];valj二valj[subkey[si]];}else{if((!subkey&&subkey!二二subkey二二''&&object,sort)varvali二obi;varvalj二obj;}else{varvali=obi[subkey];varvalj二obj[subkey];if(desc){if(valj>vartmpkeys[i]keys[j]}}else{if(valj9、;=tmp;vali){二keys[i];=keys[j];=tmp;}//is!arrayif(is_array){returnobject;}else{varsorted二{};for(vari=;i10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
5、
6、subkev==&&object・sort){varvali二object[i];varvalj二object[j];}else{varvali=object[i][subkey];varvalj=object[j][subkey];}}if(desc){if(valj>vali){vartmp=object[i];object[i]
7、=object[j];object[j]二tmp;}}else{if(valj8、;si++){vali=vali[subkey[si]];valj二valj[subkey[si]];}else{if((!subkey&&subkey!二二subkey二二''&&object,sort)varvali二obi;varvalj二obj;}else{varvali=obi[subkey];varvalj二obj[subkey];if(desc){if(valj>vartmpkeys[i]keys[j]}}else{if(valj9、;=tmp;vali){二keys[i];=keys[j];=tmp;}//is!arrayif(is_array){returnobject;}else{varsorted二{};for(vari=;i10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
8、;si++){vali=vali[subkey[si]];valj二valj[subkey[si]];}else{if((!subkey&&subkey!二二subkey二二''&&object,sort)varvali二obi;varvalj二obj;}else{varvali=obi[subkey];varvalj二obj[subkey];if(desc){if(valj>vartmpkeys[i]keys[j]}}else{if(valj9、;=tmp;vali){二keys[i];=keys[j];=tmp;}//is!arrayif(is_array){returnobject;}else{varsorted二{};for(vari=;i10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
9、;=tmp;vali){二keys[i];=keys[j];=tmp;}//is!arrayif(is_array){returnobject;}else{varsorted二{};for(vari=;i10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
10、n_offlinc":,session_online:,〃・丄〃session_count:},—c:{"scssion_offlinc":,session_online:,〃・丄〃session_count:},—〃d〃:{"scssion_of
此文档下载收益归作者所有