JavaScript sort () method

Source: Internet
Author: User

<! DOCTYPE html>

>

>

<meta CharSet="Utf-8">

<title></title>

>

<body>

</body>

<script type= "Text/javascript">

the//sort () method is used to sort the elements of the array.

//Syntax: Arrayobject.sort (sorty)

//sortby: Optional. Specifies the sort order. Must be a function.

//return value, a reference to an array, note that arrays are sorted on the original array, and no replicas are generated.

//note: If a parameter is not used when calling the method, the elements in the array are sorted alphabetically

//To sort, to say the exact point, is to sort by character encoding order. To implement

///This, you should first convert the elements of the array to a string (if necessary),

//For comparison.

//If you want to sort by other criteria, you need to provide a comparison function that

//To compare two values, and then return a relative order that describes the two values, comparing

the//function should have two parameters A and B with the following return values:

//If A is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.

//If a equals B, the value 0 is returned.

//If A is greater than B, a value greater than 0 is returned.

//1. Create an array and sort alphabetically.

var arr1 = new Array (6);

arr1[0] = "George";

arr1[1] = "John";

arr1[2] = "Thomas";

arr1[3] = "James";

arr1[4] = "Adrew";

arr1[5] = "Martin";

document.write (arr1 + "<br/>");

document.write (Arr1.sort () + "<br/>");

var arr2 = new Array (6);

arr2[0] = "ten";

arr2[1] = "5";

arr2[2] = "Max";

arr2[3] = "+";

arr2[4] = "n";

arr2[5] = "1";

document.write (arr2 + "<br/>");

document.write (Arr2.sort () + "<br/>");

The above code does not sort the numbers by the size of the numeric values, and to achieve this, a sort function must be used:

function Sortnumber(a, b) {

return A- b;

}

var arr3= new Array (6);

arr3[0] = "ten";

arr3[1] = "5";

arr3[2] = "Max";

arr3[3] = "+";

arr3[4] = "n";

arr3[5] = "1";

document.write (arr3 + "<br/>");

document.write (Arr3.sort (sortnumber)+ "<br/>");

</script>

>

JavaScript sort () method

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.