Peter’s blog ✴ Week 360 ✴ 9 February 2026

THE WEEKLY CHALLENGE
Padding and sorting

The Perl Camel

Task 2

Word sorter

You are give a sentence. Write a script to order the words in the given sentence alphabetically but keeping the words themselves unchanged.

Examples


Example 1
Input: $str = 'The quick brown fox'
Output: 'brown fox quick The'

Example 2
Input: $str = 'Hello    World!   How   are you?'
Output: 'are Hello How World! you?'

Example 3
Input: $str = 'Hello'
Output: 'Hello'

Example 4
Input: $str = 'Hello, World! How are you?'
Output: 'are Hello, How World! you?'

Example 5
Input: $str = 'I have 2 apples and 3 bananas!'
Output: '2 3 and apples bananas! have I'

Analysis

The principle behind my solution is to split the sentence into words and then create a hash like this:

$words{lc($word)} = $word;

I can then sort keys %words and output $words{$key}, ie sort the words on their lower-cased version and output the original version.

But that doesn't work if there's a repeated word, as the second occurrence will overwrite the first in the hash. To avoid that, I append a unique number to the hash key, like this:

$words{lc($1) . ++ $i} = $1

So if the sentence is 'two cats, two dogs' the hash keys will be 'two1', 'cats2', 'two3', 'dogs4'.

As always, I tested it with the edge cases of no words and just one word.

Perl Weekly’s review

from PW issue 760

The post presents the Text Justifier and Word Sorter tasks clearly with well-explained inputs and desired outputs, giving readers a solid grounding in the problem definitions. The examples are practical and show the expected string centering and alphabetical ordering behavior in a way that supports straightforward implementation.

Try it 

Try running the script with any input:



example: This challenge is not too hard

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-02-09
use utf8;     # Week 360 - task 2 - Word sorter
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

word_sorter('The quick brown fox');
word_sorter('Hello    World!   How   are you?');
word_sorter('Hello');
word_sorter('I have 2 apples and 3 bananas!');
word_sorter('one two three, one two three');
word_sorter('');
word_sorter('justoneword');

sub word_sorter {
    
    my ($sentence, %words, $key, $output, $i);
    
    # initialise
    $sentence = $_[0];
    
    # make eg $words{fred} = 'Fred'
    $words{lc($1) . ++ $i} = $1 while $sentence =~ m|([^\s]+)|g;
    
    # sort by key and concatenate values
    $output = '';
    for $key (sort{$a cmp $b} keys %words) {
        $output .= $words{$key} . ' ';
    }
    
    say qq[\nInput:  '$sentence'];
    say qq[Output: '] . substr($output, 0, -1) . q['];
}

9 lines of code

Output from script


Input:  'The quick brown fox'
Output: 'brown fox quick The'

Input:  'Hello    World!   How   are you?'
Output: 'are Hello How World! you?'

Input:  'Hello'
Output: 'Hello'

Input:  'I have 2 apples and 3 bananas!'
Output: '2 3 and apples bananas! have I'

Input:  'one two three, one two three'
Output: 'one one three, three two two'

Input:  ''
Output: ''

Input:  'justoneword'
Output: 'justoneword'

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain