Lesson 21 of 24

Lambdas and Custom Sorting

10 min 35 XP

A lambda is a small unnamed function: [capture](params) { body }. It's perfect for handing your own rule to sort, count_if or for_each.

Example: edit me and press Run

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.

Challenge

+35 XP

Read n and n words. Sort them by length (shortest first), breaking ties alphabetically, and print one per line. Input: 5 banana kiwi apple fig cherry Output: fig kiwi apple banana cherry

Output Loading C++ (first time takes a few seconds)
Press Run to see the result here.