To implement the sort method of an array

Source: Internet
Author: User
Tags javascript array

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

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.