TypeScript bootcamp · Lab 26

Sort by length

mediumArrow Functions12 minLesson: Lambdas

Read the question, write TypeScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read words; print them sorted by length (stable), space-separated.

Input. Space-separated words.

Output. Words by length.

Examples

Example 1
Input
cat elephant a dog
Output
a cat dog elephant
Hint
  1. [...words].sort((a,b) => a.length - b.length)
Show correct code

Peek only after you have tried. You can still Check your own version.

const words = readLine().trim().split(/\s+/);
console.log([...words].sort((a, b) => a.length - b.length).join(' '));
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.