Sort words in string with Perl

This example shows how to sort words in a string:

my $str = 'bc ab cd ac';

print $str.$/;
$str = sortString($str);
print $str.$/;

sub sortString {
    return join " ",sort {$a gt $b} split / /,$_[0];

And as result we get this:

bc ab cd ac 
ab bc ac cd

One thought on “Sort words in string with Perl”

  1. When someone writes an article he/she retains the idea of a
    user in his/her brain that how a user can know it.
    Therefore that’s why this piece of writing is perfect. Thanks!

Leave a Reply

Your email address will not be published.

12 + 13 =