Course of Raku / Advanced / Subroutines / Calling with a colon 🆕 / Exercises / Sort with a colon

Solution: Sort with a colon

Here is a possible solution to the task.

Code

my @words = <pear fig apple kiwi>;
say @words.sort: *.chars;

🦋 You can find the source code in the file colon-sort.raku.

Output

(fig pear kiwi apple)

Comments

  1. The colon passes the sort key to sort without needing parentheses around it.

  2. *.chars is a Whatever expression that stands for “the number of characters of each element”. sort uses it as the key, so the words come out shortest first: fig (3), then pear and kiwi (4), then apple (5).

  3. pear keeps its place ahead of kiwi because they are the same length and sort is stable, preserving the original order of equal keys.

Course navigation

Sort with a colon   |   Quiz — The colon call