标签:cti rip nbsp script 返回值 json str else log
1、a-b升序
<script> var arr = [2, 3, 1, 5, 0]; function compare(a, b) { return a - b; } console.log(arr.sort(compare));//(5) [0, 1, 2, 3, 5] </script>
2、b-a降序
<script> var arr = [2, 3, 1, 5, 0]; function compare(a, b) { return b - a; } console.log(arr.sort(compare));//(5) [5, 3, 2, 1, 0] </script>
3、返回值:1升序,-1降序
<script> var arr = [2, 3, 1, 5, 0]; function compare(a, b) { if (a > b) { return -1; } else if (a < b) { return 1; } else { return 0; } } console.log(arr.sort(compare));//(5) [5, 3, 2, 1, 0] </script>
标签:cti rip nbsp script 返回值 json str else log
原文地址:http://www.cnblogs.com/wuqiuxue/p/7852835.html