Js object array by attribute quick sorting _ javascript skills

Source: Internet
Author: User
As mentioned in the previous article about selector performance competitions, visual testing considers that when $ (& quot; div, p, a & quot;) has a comma, sizzle has an exception in time consumption (more than 600 elements, which took 200 ms), saying that it may not optimize the sorting in ie. According to the recommended program run under IE, indeed, the sorting time is very small.

The Code is as follows:


Script
/*
* Shuffling
*/
Function getRandomPlayCard (m ){
Var array1 = new Array (m );
For (var I = 0; I Var rnd = Math. floor (Math. random () * (I + 0.99999 ))
Array1 [I] = array1 [rnd];
Array1 [rnd] = I;
}
Return array1;
};
/*
* Fast sorting: Sorting by a certain attribute or by the function for obtaining the sort basis.
* @ Method soryBy
* @ Static
* @ Param {array} arr array to be processed
* @ Param {string | function} sort by property to obtain
* @ Param {boolean} desc descending order
* @ Return {array} returns the new sorted array.
*/
Var sortBy = function (arr, prop, desc ){
Var props = [],
Ret = [],
I = 0,
Len = arr. length;
If (typeof prop = 'string '){
For (; I Var oI = arr [I];
(Props [I] = new String (oI & oI [prop] | ''). _ obj = oI;
}
}
Else if (typeof prop = 'function '){
For (; I Var oI = arr [I];
(Props [I] = new String (oI & prop (oI) | ''). _ obj = oI;
}
}
Else {
Throw' parameter Type Error ';
}
Props. sort ();
For (I = 0; I Ret [I] = props [I]. _ obj;
}
If (desc) ret. reverse ();
Return ret;
};
For (var I = 0; I <1000; I ++ ){
Document. write ('

A' + I +'

')
}
Var els = document. getElementsByTagName ('P ');
Var cards = getRandomPlayCard (els. length );
Var randomEls = [];
For (var I = 0, len = cards. length; I Alert (['total: ', randomEls. length,' after disordered order: ', randomEls [0]. innerHTML, randomEls [randomEls. length-1]. innerHTML]);
Var d0 = new Date ();
Var elsSorted = sortBy (randomEls, function (el) {return el. sourceIndex + 100000000 ;})
Alert (['total: ', elsSorted. length, 'Time consumed for sorting: ', new Date ()-d0, 'after re-sorting:', elsSorted [0]. innerHTML, elsSorted [elsSorted. length-1]. innerHTML]);
Script


The native sort of Array. When it transmits a comparison function, it takes a lot of time because of the sort algorithms used internally.
The preceding quick sorting is not compared multiple times,
But:
1. Get the el attribute value and use the attribute value to generate a String object,
2. Attaches el to a String object.
3. An array composed of String objects.
4. Use native sort to sort the String object array.
5. In the sorted String array, el is retrieved in order.
The sorted el array is obtained.
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.