js 数组随机排序

仅用于个人学习记录

javascript 数组随机排序
1.最简洁的方法:
function randomsort(a, b) {
    return Math.random()>.5 ? -1 : 1; //用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);

2.数组中的sort方法排序 无参数是按照字符编码的顺序进行排序。
arrayObject.sort(sortby)

function sortby(a,b) {
    return a < b ? -1 : 1;//如果a<b不交换,否则交换,即升序排列
}

function sortby(a,b) {
    return a > b ? -1 : 1;;//如果a>b不交换,否则交换,即将序排列
}

原文地址:https://www.cnblogs.com/ilovexiaoming/p/3229227.html