教程 radash tutorial

sort

按数值属性对对象列表进行排序

基本用法

给定对象数组,返回一个按get函数中指定的数值属性排序的新数组。第三个可选参数允许按降序排序,而不是默认的升序排序。

此函数仅支持数值排序。对于字母排序,请参见alphabetical函数。

ts
import { sort } from 'radash'

const fish = [
  {
    name: 'Marlin',
    weight: 105
  },
  {
    name: 'Bass',
    weight: 8
  },
  {
    name: 'Trout',
    weight: 13
  }
]

sort(fish, f => f.weight) // => [bass, trout, marlin]
sort(fish, f => f.weight, true) // => [marlin, trout, bass]