Skip to content

sortBy

sortBy(array, keyFn) → array · Array

Sort an array using a key extractor function.

  • array (required): the array to sort

  • keyFn (required): lambda (element) -> sortKey

bash
echo '{"products": [{"name": "Widget", "price": 25}, {"name": "Gadget", "price": 150}, {"name": "Gizmo", "price": 10}]}' | utlx -e 'sortBy($input.products, (p) -> p.price)'
# [{"name": "Gizmo", "price": 10}, {"name": "Widget", "price": 25}, {"name": "Gadget", "price": 150}]
utlx
{
  cheapestFirst: sortBy($input.products, (p) -> p.price),
  expensiveFirst: sortBy($input.products, (p) -> -p.price),
  alphabetical: sortBy($input.products, (p) -> p.name)
}

Released under AGPL-3.0.