This paper illustrates the method of random ordering of JavaScript array. Share to everyone for your reference. Specifically as follows:
We're going to test the random sort of 0-9, Sir into the data
var arr=[9,3,1,2,5,8,4,7,6,0];
Arr.sort ();
document.write ("The array element after the normal sort:" +arr.join (",") + "<br/>");
Array elements after a normal sort: 0,1,2,3,4,5,6,7,8,9
Define a random function, return a positive or negative number randomly, and the sort function will think that the 2 values are positive or reverse according to the random return of the positive or negative.
function Randomsort (A, b) {return
math.random ()-0.5;
}
Arr.sort (randomsort);
document.write ("Random sort of array elements:" +arr.join (",") + "<br/>");
Random sort of array elements: 3,1,7,4,8,6,2,0,9,5
Random sort of array elements: 8,3,1,9,5,0,7,6,4,2
I hope this article will help you with your JavaScript programming.