Summary of methods to randomly disrupt arrays in JS, and summary of js disruption

Source: Internet
Author: User

Summary of methods to randomly disrupt arrays in JS, and summary of js disruption

This example summarizes the methods for randomly disrupting arrays in JS. We will share this with you for your reference. The details are as follows:

In JS, there are many methods to disrupt arrays. I think it is the most streamlined method to spread a method written by a foreign user on the Internet:

Function randomsort (a, B) {return Math. random ()>. 5? -1: 1; // use the Math. random () function to generate 0 ~ Returns-1 or 1} var arr = [1, 2, 3, 4, 5]; arr. sort (randomsort) when comparing the random number between 1 and 0.5 );

Here we will introduce the sort () function. In JS, an Array object has a built-in function:

arrayobj.sort([sortfunction])

This method sorts Array objects appropriately. During execution, no new Array objects are created.

SortFunction is optional.

Is the name of the function used to determine the order of elements. If this parameter is omitted, the elements are listed in ascending order of ASCII characters.

The sortFunction method has two parameters. Represent the two array items for each sort comparison. This parameter is executed every time two array items are compared during sort () sorting, and two compared array items are passed as parameters to this function. When the return value of the function is 1, the order of two array items is exchanged. Otherwise, the order is not exchanged.

We can slightly modify the preceding randomsort () to implement ascending and descending sorting:

Function asc (a, B) {return a <B? -1: 1; // If a <B is not exchanged, otherwise, the exchange is in ascending order} function desc (a, B) {return a> B? -1: 1; // If a> B is not exchanged, otherwise, it will be sorted in sequence}

In addition, an unnamed function can be directly put into the call of the sort () method. In the following example, the odd number is placed at the top and the even number is placed at the bottom. The example is as follows:

The following is a reference clip:

Var arrA = [6, 2, 4, 3, 5, 1]; arrA. sort (function (x, y) {if (x % 2 = 0) return 1; if (x % 2! = 0) return-1;}); document. writeln (arrA); // output :,

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.