JavaScript in sort-sorted instances _javascript Tips

Source: Internet
Author: User

The code looks like this:

var arr = [5,32,28,66,2,15,3];
  Arr.sort (function (A1,A2) {return 
   a1-a2;//a2-a1 input Reverse
  });
  Console.log (arr);
  Console.log (Arr.reverse ()); Reverse reverse the order of elements in an array

 var arr2 = [' Hezihao ', ' Chensan ', ' xiaomin ', ' Lishi ']
  arr2.sort ();
  Console.log (ARR2);

 var arr3 = [' 6 ', ' 8 ', ' 0 ', ' 7 ']
  arr3.sort ();
  Console.log (ARR3);

The above code enters the result:

Arrays are sorted alphabetically without passing in parameters

Sort numbers by their size, and to do this, you have to use a sort function,

If the A1-A2 is a negative number, ascending, or descending if A2-A1 is a positive number, or you can use the reverse () method to reverse the order of the elements in the array

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.