API Reference

Runs a callback function on array of items and returns an array of items that are sorted by the return value of a callback function on the item. This function utilizes the safeSort method under the hood.

function sortBy<T>(things: T[], func: Function)

// Example:

const socialStats = [
  { follows: 1, likes: 2 },
  { follows: 5, likes: 1 },
  { follows: 2, likes: 0 },
  { follows: 4, likes: 3 },
]

// Adds the follows and likes to get a total popularity score
const getPopularity = (obj: { follows: number; likes: number }) => obj.follows + obj.likes

sortByCallbackResult(socialStats, getPopularity)
//=>
[
  { follows: 2, likes: 0 },
  { follows: 1, likes: 2 },
  { follows: 5, likes: 1 },
  { follows: 4, likes: 3 },
]