Array sorting functions of JavaScript objects and six usages _ javascript skills

Source: Internet
Author: User
This article will share with you a function for sorting arrays or objects. This function can sort arrays or elements by any depth array or object value as the sorting base and share a function for sorting arrays or objects. This function can sort the array or element with any depth array or object value as the sorting base.

The Code is as follows:

/*** Sort the array or object ** by Jinko * date -- * @ param object array or object * @ param subkey, which can be a string, it can also be an array * @ param desc sorting method, true: descending, false | undefined: ascending * @ returns {*} returns the sorted array or object ** note: for sorting objects, if you use console. the display of log printing objects may be inconsistent with the sorting results. * The keys of log printing objects are displayed in alphabetical order by the browser, but in the for loop, it is the correct sorting order */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
 
  
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 {// object sorting 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
  
   
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
   
    

The usage is 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" :}}; // sort data = sort_object (data, 'session _ online') in ascending order based on the session_online field; for (var k in data) {console. log (data [k]);} console. log ('------------------'); // sort data = sort_object (data, 'session _ offline', true) in descending order based on the session_offline field; 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" :,}]; // sort data = sort_object (data, 'cpuhz _ use') according to the cpuhz_use field; console. log (data );

Usage 3:

Var data = [,]; // sort one-dimensional arrays in ascending order. data = sort_object (data); console. log (data); // sort one-dimensional arrays in descending order data = sort_object (data, null, true); console. log (data );

Usage 4:

Var data = {'A':, 'B':, 'C':, 'D':}; // sort objects in ascending order. data = sort_object (data ); // sort objects in descending order 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 ":}}}; // sort the values of l under the object element in ascending order. data = sort_object (data, ['l', 'L']); for (var k in data) {console. log (data [k]. l );}

Usage 6:

Var data = [[{"a" :},,], [{"a" :},,], [{"a" :},,]; // sort data = sort_object (data, [, 'a']) in ascending order based on the key name a of the elements below the array. for (var k =; k
     
      

The above content is a summary of the JavaScript Object array sorting function and six usage. I hope you will like it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.