Skip to content

alphabetical

按属性对对象数组进行字母顺序排序

186 bytes
since v12.1.0

使用方法

给定一个对象数组和一个用于确定排序属性的回调函数,返回按字母顺序排序的新数组。第三个可选参数允许您按降序排序,而不是默认的升序排序。

对于数值排序,请参见 sort 函数。

import * as _ from 'radashi'
const gods = [
{
name: 'Ra',
power: 100,
},
{
name: 'Zeus',
power: 98,
},
{
name: 'Loki',
power: 72,
},
{
name: 'Vishnu',
power: 100,
},
]
_.alphabetical(gods, g => g.name) // => [Loki, Ra, Vishnu, Zeus]
_.alphabetical(gods, g => g.name, 'desc') // => [Zeus, Vishnu, Ra, Loki]