JavaScript Object array sorting function and six usage _javascript tips

Source: Internet
Author: User
Tags array sort print object

Share a function that is used to sort an array or an object. The function can be sorted by an array of any depth or by the value of an object as the element of the sorted cardinality.

The code is as follows:

 /** * Sort array or Object * by Jinko * Date--* @param object array or Object * @param subkey need to sort the subkey, which can be a string or an array * @pa Ram desc Sort By, true: Descending, false|undefined: Ascending * @returns {*} returns sorted array or Object * Note: For object sorting, if using Console.log Print object display possible and sort knot 
   Inconsistent, * The key is displayed alphabetically by the browser, but in the for loop it is the correct sort 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<keys.length; i++) {for (Var j=i+; j<keys.length; J + +) {if (Is_array) {
           Array sort 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 {//object sort 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  

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 by session_online character Che sequence
 = sort_object (data, ' session_online ');
 For (var k in data) {
   console.log (data[k]);
 }
 Console.log ('------------------');
 Sort data by Session_offline field
 in descending order = 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":,
   }
 ];
 Sort data by cpuhz_use field
 = Sort_object (data, ' cpuhz_use ');
 

Usage 3:

 var data = [,,,,,,,];
 Sort a one-dimensional array in ascending order
 data = sort_object (data);
 Console.log (data);
 Sort the one-dimensional array in descending order
 data = sort_object (data, NULL, TRUE);
 

Usage 4:

 var data = {' A ':, ' B ':, ' C ':, ' d ':};
 Sort the object 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"
 :}}}; The value of L under L of an object element is sorted 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": 
     },
     ,
   ]
 ];
 The elements of an array are sorted in ascending order based on the value of the key of the element named a,
 data = sort_object (data, [, ' a ']);
 for (var k =; k<data.length; k++) {
   console.log (data[k]);
 Console.log ('---------------------');
 The elements of an array are sorted in ascending order based on the values of the elements marked as
 data = sort_object (data, []);//equivalent to data = sort_object (data);
 for (var k =; k<data.length; k++) {
   console.log (data[k]);
 

The above content is small to share the JavaScript object array sorting function and six usage of all narration, hope you like.

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.