The sort function in JavaScript

Source: Internet
Author: User
Tags sort

The sort function method in JavaScript is to return an Array object that has been sorted by an element. How to use:


Where Arrayobj is a required option. Any Array object.

Sortfunction is optional. is the name of the function used to determine the order of elements. If this argument is omitted, then the elements are sorted in ascending order in ASCII characters.

The sort function method in JavaScript sorts the array object appropriately, and does not create a new array object during execution.

If you provide a function for the sortfunction parameter, the function must return one of the following values:

Negative value if the first argument passed is smaller than the second argument.

0 if two parameters are equal.

Positive value if the first argument is larger than the second argument.


The following example illustrates the use of the sort function method in javascript:

  function SortDemo(){
 var a, l;              // 声明变量。
 a = new Array("X" ,"y" ,"d", "Z", "v","m","r");
 l = a.sort();            // 排序数组。
 return(l);              // 返回排序的数组。

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: 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.