Prism · Real world

The top of a list

A leaderboard: sort by a field, highest first, and keep the first three.

The source
{
  players: [
    { name: 'Ada', score: 187 },
    { name: 'Grace', score: 245 },
    { name: 'Linus', score: 132 },
    { name: 'Margaret', score: 298 },
    { name: 'Donald', score: 91 },
    { name: 'Tim', score: 211 },
  ],
}
The config
{
  $slice: {
    from: {
      $sortBy: {
        over: { $ref: '$.players' },
        as: 'p',
        by: { $get: { from: { $var: 'p' }, path: ['score'] } },
        dir: 'desc',
      },
    },
    start: 0,
    end: 3,
  },
}
What comes out
[
  { name: 'Margaret', score: 298 },
  { name: 'Grace', score: 245 },
  { name: 'Tim', score: 211 },
]
Output matches the expected value

Runs in this page on the installed package. The input and the expected output are defined by the example.