Array
Sorts an array of objects alphabetically by a property
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 { alphabetical } from 'radash'
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]