JS Multiple sorting

Source: Internet
Author: User

1 <!DOCTYPE HTML>2 <HTML>3 <Body>4 <Script>5     varArray= NewArray ();6      for(varI= 0; I<Ten; I++)7     {8 Array[i]= NewArray ();9 array[i][0] =Math.floor (Math.random ()*Ten);Ten array[i][1] =Math.floor (Math.random ()*Ten); One     } A document.write ("before sorting:"); - display (); - Array.Sort (functionCompare (V1,V2) the     { -         if(v1[0] >v2[0]) -         { -             return 1; +         } -         Else if(v1[0] <v2[0]) +         { A             return -1; at         } -         Else -         { -             if(v1[1]>v2[1]) -             return 1; -             Else if(v1[1]>v2[1]) in             return -1; -             Else to             return 0; +         } -     }); the document.write ("after sorting:"); * display (); $     functiondisplay ()Panax Notoginseng     { -          for(varI= 0; I<Ten; I++) the         {    + document.write ("<br>"); A document.write (" "); the document.write (array[i][0]); + document.write (" "); - document.write (array[i][1]); $         } $     } - </Script> - </Body> the </HTML>
View Code

The sort function in JS needs to pass in a function, such as sort (function (x, y) {}). X, Y is the two value of the array that needs to be sorted, and can be returned according to the size of X, Y:

A negative value, if the first argument passed is smaller than the second one.

0, if two parameters are equal.

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

If positive, the X, y order is swapped, and 0 or-1 does not swap for x, Y.

JS Multiple sorting

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.