通过Math.random()方法,实现数组顺序的随机排序
function randomsort(a, b) {
undefined
return Math.random()>0.5 ? -1 : 1;//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
var arr = ["鹿晗", "王俊凯", "蔡徐坤", "彭于晏", "周杰伦", "刘德华", "赵本山"];
console.log(arr.sort(randomsort));
很巧妙的运用了sort方法