Home  >  Article  >  Web Front-end  >  js object array quick sorting by attributes_javascript skills

js object array quick sorting by attributes_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:11:101169browse

I ran it under IE according to the recommended program. Indeed, the sorting time is very small.

Copy code The code is as follows:

<script> <br>/* <br> * Shuffle <br>*/ <br>function getRandomPlayCard(m){ <br>var array1=new Array(m); <br>for(var i=0;i<m;i ){ <BR>var rnd=Math.floor(Math.random()*(i 0.99999)) <BR>array1[i]=array1[rnd]; <BR>array1[rnd]=i; <BR>} <BR>return array1; <BR>}; <BR>/* <BR>* Quick sort, sort by an attribute or by "function to get sorting basis". <BR>* @method soryBy <BR>* @static <BR>* @param {array} arr Array to be processed<BR>* @param {string|function} prop Sort by attribute, get<BR>* @param {boolean} desc Descending order<BR>* @return {array} Return sorting The new array after <BR>*/ <BR>var sortBy =function (arr, prop, desc){ <BR>var props=[], <BR>ret=[], <BR>i=0, <BR>len=arr.length; <BR>if(typeof prop=='string') { <BR>for(; i<len; i ){ <BR>var oI = arr[i]; <BR>(props [i] = new String(oI && oI[prop] || ''))._obj = oI; <BR>} <BR>} <BR>else if(typeof prop=='function') { <BR> for(; i<len; i ){ <BR>var oI = arr[i]; <BR>(props[i] = new String(oI && prop(oI) || ''))._obj = oI; <BR>} <BR>} <BR>else { <BR>throw 'Wrong parameter type'; <BR>} <BR>props.sort(); <BR>for(i=0; i<len; i ) { <BR>ret[i] = props[i]._obj; <BR>} <BR>if(desc) ret.reverse(); <BR>return ret; <BR>}; <BR>for( var i=0;i<1000;i ){ <BR>document.write('<div>a' i '</div>') <br>} <br>var els=document.getElementsByTagName(' div'); <br>var cards=getRandomPlayCard(els.length); <br>var randomEls=[]; <br>for(var i=0,len=cards.length;i<len;i ) randomEls[ cards[i]]=els[i];//Shuffle the element array according to the shuffled cards<BR>alert(['Total number: ',randomEls.length,'After shuffling the order: ',randomEls[0]. innerHTML,randomEls[randomEls.length-1].innerHTML]); <BR>var d0=new Date(); <BR>var elsSorted=sortBy(randomEls,function(el){return el.sourceIndex 100000000;}) <BR>alert(['Total number:',elsSorted.length,'Sort time-consuming:',new Date()-d0,'After resorting: ',elsSorted[0].innerHTML,elsSorted[elsSorted.length-1] .innerHTML]); <BR></script>

Array’s native sort, when it passes a comparison function, it requires multiple comparisons due to the sorting algorithm it uses internally. , so it is natural that it takes time.
The quick sort above does not have multiple comparisons,
but:
1. Take out the el attribute value and use the attribute value to generate a String object,
2. Append el to the String object.
3. Use String objects to form an array.
4. Use native sort to sort the String object array.
5. In the sorted String array, remove el in order.
You will get the sorted el array.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn