数组反转

const arr = [10.2,30,1,4,7,3,12,33,40]

let a = arr.reverse()

console.log(a) // [40,33,12,3,7,4,1,30,10.2]

数组排序sort()

const arr = [10.2,30,1,4,7,3,12,33,40]

// sort 按照编码排序,不推荐,所有经常传一个比较函数作为参数
let a = arr.sort()

console.log(a) // [1,10.2,12,3,30,33,4,40,7]

普通数组排序

const arr = [10.2,30,1,4,7,3,12,33,40]

function compare(value1,value2) {
  if(value1 < value2) {
    return -1
  } else if(value1 > value2) {
    return 1
  } else {
    return 0
  }
}

let a = arr.sort(compare)

console.log(a) // [1,3,4,7,10.2,12,30,33,40]

数组对象排序

var arr = [{name: "zlw", age: 24}, {name: "wlz", age: 25}];
var compare = function (obj1, obj2) {
    var val1 = obj1.age;
    var val2 = obj2.age;
    if (val1 < val2) {
        return -1;
    } else if (val1 > val2) {
        return 1;
    } else {
        return 0;
    }            
} 
console.log(arr.sort(compare));