We will test the random sorting of 0-9, Mr into data viewsourceprint? 1 vararr [,]; viewsourceprint? 1arr. sort (); 2document. write (& quot; array element after normal sorting: & quot; + arr. join (& quot;, & quot;) + & quo
We will test the random sorting of 0-9, Mr into data
View sourceprint? 1 var arr = [,];
View sourceprint? 1 arr. sort ();
2 document. write ("array elements after normal sorting:" + arr. join (",") +"
");
Array elements after normal sorting: 0, 1, 2, 3, 4, 5, 6, 8, 9
Define a random function that returns positive or negative numbers at random. The sort function determines whether the two values are in positive or reverse order based on the positive or negative values returned by random.
View sourceprint? 1 function randomSort (a, B ){
2 return Math. random ()-0.5;
3}
4
5 arr. sort (randomSort );
6 document. write ("array elements after random sorting:" + arr. join (",") +"
");
Array elements after random sorting: 3, 1, 7, 4, 8, 6, 2, 0, 9, 5
Array elements after random sorting: 8, 3, 5, 0, 7, 6, 4, 2
Author: "blog smaller"