数组排序

JS:
// 数组从小到大排序 var a = [9, 2, 33, 66, 55, 56, 34, 1, 91, 88]; for (var i = 0; i < a.length; i++) { for (var j = i + 1; j < a.length; j++) { if (a[i] > a[j]) { let tem = a[i] a[i] = a[j]; a[j] = tem; } } } console.log(a) // [1, 2, 9, 33, 34, 55, 56, 66, 88, 91] // 数组从大到小排序 var array = [9, 2, 33, 66, 55, 56, 34, 1, 91, 88] for (var i = 0; i < array.length; i++) { for (var j = i + 1; j < array.length; j++) { if (array[i] < array[j]) { let d = array[i]; array[i] = array[j]; array[j] = d; } } } console.log(array) // [91, 88, 66, 56, 55, 34, 33, 9, 2, 1]

  

posted @ 2018-05-02 11:14  杏杏子  阅读(119)  评论(0编辑  收藏  举报