Array and numeric sorting of JavaScript

Source: Internet
Author: User
Tags arrays integer numeric sort javascript array

JavaScript array allocation memory is non-linear, which sacrifices performance, the advantage is more flexible, for example: array elements do not require the same type, add elements to the array does not cross.

The array subscript starts at 0 and the length value is the maximum subscript plus 1. The array subscript does not have to be continuous, you can also change the length of the value, the array element memory is dynamically allocated, just set larger length does not occupy more space, and the value of length is changed to small, then the element is greater than or equal to the subscript will be cleared out, memory released.

The main differences between arrays and objects are two points, one is that the Array object property name cannot be customized, and the second is that the array object will have some method of handling the array, not in the object.

adding element syntax using the push function is simpler than giving, for example, Words.push ("Go") adds an element "go" to the tail of the words array, and length automatically adds one.

Deleting an array element using the Delete function, such as delete words[2, deletes the 3rd element. If the deletion is not the last array element, it causes the array subscript to be discontinuous. The splice method produces sequential results, using examples: words.splice[3,2], starting with an element with an ordinal number of 3, deleting 2 elements, and moving the remaining elements 2 places (beware of the performance problems of the array elements in particular).

Traversing an array through a for in statement does not guarantee order, and may also get unexpected properties from the prototype chain. You should generally pass the for (var i = 0;i<myarray.length;i+=1) {do somthing ...} Such statements to enumerate the arrays.

Whether an array or an object is used depends primarily on the attribute name of the application requirement is not an integer. JavaScript cannot differentiate between arrays and objects with TypeOf, both of which return "object". The code to validate the array type is as follows:

var Is_array = function (value) {return     
    value &&//typeof value
        = = = "Object" &&//typeof detect type as OB Ject
        typeof value.length = = "number" &&//length attribute is number type
        typeof Value.splice = = "function" && Amp There are splice ways
        ! ( Value.propertyisenumerable ("Length"))//length is an enumerable property
}

Because the array sorting method (sort) is compared by string, the result of sorting the numbers is usually not as expected. Most people think about rewriting numbers, and I think it's better to sort numbers in front of the number 0, after the sequence, and then take the zeros off. This method bypasses the difficulty of the idea sorting algorithm, and also can break the limit of the number length, sort the large number and play the performance of the built-in algorithm.

Number 0, positive integer aligned Array.prototype.lpad = function (n) {var lpad1 = function (num, n) {var len = Math.floor (

         num). toString (). length;
             while (Len < n) {num = "0" + num;
         len++;
    return num;
    } var tmparray=[];
    for (Var i=0;i<this.length;i+=1) {Tmparray.push (Lpad1 (this[i],n));
return tmparray;  }//Sort Array.prototype.nsort = function (n) {var farray = [];//less than 01 group var Zarray = [];//greater than or equal to 01 group of var tmp = []; Temporary array var result = [];//return value for (Var i=0;i<this.length;i+=1) {if (this[i]<0) {FARRAY.P
        Ush (Math.Abs (this[i));//Negative numbers}else{Zarray.push (this[i]);     
    } tmp = Farray.lpad (n). sort ();     

    for (Var i=0;i<tmp.length;i+=1) {Result.push (parsefloat (tmp[tmp.length-1-i)) *-1);//negative groups to be sorted in reverse}     
    TMP = Zarray.lpad (n). sort ();     
        for (Var i=0;i<tmp.length;i+=1) {Result.push (parsefloat (tmp[i));     
return result;     

} Mynumarray = [2,-80,-9.6,-9.4,-10,5.823,-13.888,20,312,3,2,55,-1,7.999,22];     
ijs.pt ("Mynumarray");     
ijs.pt ("Is_array (Mynumarray)");     
ijs.pt ("Mynumarray.sort ()"); ijs.pt ("Mynumarray.nsort (100)");

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.