JavaScript Array.Sort method can pass a function as the sorting standard, tonight is sitting on the balcony in a daze with his wife, there is a sentence without a chat, bored to achieve a sort play.
The code is as follows:
Array.prototype.sort2 = function (callback) { var = this; for (var i = self.length-1, I >0; i--) {for (var j = 0; J < i; J + +) { if (callback (Self[j],self[j+1])) {
var t = self[j]; SELF[J]=SELF[J+1]; Self[j+1] = t; };}; return self; } var a = []; for (var i = 0; i < 10000; i++) { A.push (Math.random ()); }; var dt = new Date (); A.sort2 (function (v1,v2) {return v1>v2;}); Console.info (New Date ())-DT);
Then in the following test process, I alternately run sort2 and sort, found that is not an order of magnitude, a large number of data sort bubbling is certainly not feasible, another day try to plug in, see if we can shorten the gap with the built-in sort. I'm afraid this sort is completely written in JS's interpretation language, not written in JavaScript, and it can't be written the same way ...
To implement the sort method of an array