Implementation of array multi-conditional ordering using Javascript iterative method

Source: Internet
Author: User
Tags array length

Multi-conditional ordering may have a lot of ideas, efficiency is different, my method may only be suitable for their own use, after all, the purpose is to achieve the function, so the most stupid method, but the effect is very ideal, after several tests, 6 columns of 1000 rows of data, The average sorting time is about: 28ms.

The specific implementation code is as Follows:

functionIsarr (data) {return({}). tostring.call (data) = = ' [object Array] '; }    functiongetindex (arr) {vari = 0, Len=arr.length Keys= [];  while(i <Len) {keys.push (i++); }        returnkeys; }        //detects array maximum dimension, Non-array returns 1, if NUM indicates whether the array is a specified number of dimensions    functionCheckarrdim (arr, Num) {varDimension =-1, Num=parseint (num), Ischeck= IsNaN (num)?false:true, DM= [0], i, len, mx; if(isarr (arr) && (len = arr.length) > 0) {dimension= 1;//any array, as long as there is data, at least a 1-d             for(i = 0; I < len; i++) {dm.push (checkarrdim (arr[i)); //recursively gets the number of dimensions for each element, if the DM array is all-1 indicating that arr is a 1-dimensional array} dimension= (MX = Math.max.apply (NULL, Dm)) = = =-1? Dimension:dimension +mx; }        //if the DM array length <= 1 indicates that arr is not an array, or an empty array        //when DM array length > 1, and Dimension = = 1, indicates that arr is a 1-dimensional array        //or dimension <> 1, because DM is filled by default 0, as long as all elements AND/DM minus 0 after the length = = num-1, that is, the description is an n-dimensional array        returnischeck? (dm.length > 1?) (dimension = = 1 && num = 1) | | Eval (dm.join (' + '))/(dm.length-1) = = Num-1:false): dimension; }    functionmsort (arr, field, order) {if(!checkarrdim (arr, 2) | |!checkarrdim (field, 1) | |!checkarrdim (order, 1)) {            return ; }        varkey, tmp, val, sa, sk, pre; vari, ilen, j, jlen, k, klen, m, mlen; varRange =[], rng; //sort a pending array by an indexed array of sorted arrays        varSortfromkey =function(data, Key) {varTMP =[], i, j, len;  for(i = 0, len = key.length; i < len; i++) {tmp.push (data[key[i]); }             for(j = 0; J < len; J + +) {data[j]=tmp[j];        }        }; //multi-conditional Sorting         for(i = 0, Ilen = field.length; i < ilen; i++) {tmp=arr[field[i]]; if(i = = 0) {                //1th order, sort directly on the array where the current field is locatedKey =GetIndex (tmp);                Tmp.mergesort (key, order[i]); Range.push ([0, tmp.length-1]); } Else {                //If there is a condition of 2nd and above, the previous 1 conditions are references, and the first 1 sorted arrays are obtained                //The interval of the same element within each set of elements, assigning values to the temporary array and sorting, and getting the sort                //indexed, and finally stitched together this new indexed array is a reference to all other array sorts.                //multi-conditional sorting can be done through the above loop execution                //↓ Initial work before the core workPre = arr[field[i-1]];//top 1 sorted arraysval = pre[0]; SA= [tmp[0]]; SK= [0]; Key= []; RNG= []; //The core work of this sort is to tidy up the same range of sorted arrays, which is the only reference for multiple sorting intervals of the current array to be sorted .                 for(k = 0, Klen = range.length; k < klen; k++) {                     for(m = range[k][0] + 1, Mlen = range[k][1] + 1; m <= mlen; M++) {//Note that the conditional expression here requires additional ordering and initialization                        if(val = = = pre[m] && m!== mlen) {//no matter how small the interval, even if there are only 1 elements, when m = Mlen must perform the sort and the next initial preparationSa.push (tmp[m]);                        Sk.push (m); } Else{rng= Rng.concat ([[sk[0], sk[sk.length-1]]]);  Sa.mergesort (sk, order[i]); //mainly to obtain the SKKey =Key.concat (sk); Val=pre[m]; SA=[tmp[m]]; SK=[m]; } }} Range= rng;//get the sorting interval after finishingSortfromkey (tmp, key);//sort the current array            }            //after the previous procedure, a condition has been sorted and a sorted original index (array) is obtained            //then all other arrays except the current field array are rearranged by sorted index             for(j = 0, Jlen = arr.length; J < jlen; J + +) {                if(j = =field[i]) {                    Continue;            } Sortfromkey (arr[j], key); }        }    }

which uses the array mergesort () is its own definition to the Array.prototype method, link address: javascript-merge sort

Implementation of array multi-conditional ordering using Javascript iterative method

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.