Skip to content

alphabetical

Sorts an array of objects alphabetically by a property

186 bytes

Usage

Given an array of objects and a callback function used to determine the property to use for sorting, return a new array with the objects sorted alphabetically. A third, and optional, argument allows you to sort in descending order instead of the default ascending order.

For numerical sorting, see the sort function.

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]