function randomsort(a, b) {
return Math.random()>0.5 ? -1 : 1;//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
var arr = [0,1, 2, 3, 4, 5,6,7,8,9];
arr.sort(randomsort);

 

很巧妙的运用了sort方法

相关文章:

  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-02-11
  • 2021-12-15
  • 2022-12-23
  • 2021-04-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案