const data1 = [1, 30, 4, 20, 10]
data1.sort()
console.log(data1)

// arr.sort(
//     function (a, b) {...}
// )

const data2 = [1, 30, 4, 20, 10]
// 升序排列
data2.sort((a, b) => a - b)
console.log(data2)

const data3 = [1, 30, 4, 20, 10]
// 降序排列
data3.sort((a, b) => b - a)
console.log(data3)

const data4 = [
    { name: 'zs', age: 28, score: 90 },
    { name: 'ls', age: 38, score: 70 },
    { name: 'sb', age: 18, score: 70 }
]
data4.sort((a, b) => {
    return a.age - b.age
})
console.log(data4)

data4.sort((a, b) => {
    // 先按分数升序
    let scoreRst = a.score - b.score
    // 如果分数相同
    if (scoreRst == 0)
        // 按年龄降序
        return b.age - a.age
    return scoreRst
})
console.log(data4)

/*
# node sort.js 
[ 1, 10, 20, 30, 4 ]
[ 1, 4, 10, 20, 30 ]
[ 30, 20, 10, 4, 1 ]
[
  { name: 'sb', age: 18, score: 70 },
  { name: 'zs', age: 28, score: 90 },
  { name: 'ls', age: 38, score: 70 }
]
[
  { name: 'ls', age: 38, score: 70 },
  { name: 'sb', age: 18, score: 70 },
  { name: 'zs', age: 28, score: 90 }
]
*/
