Sort and the Schwartzian transform

Default sort

sort with no block compares elements as strings, using the cmp operator. That is what you want for names and paths, and wrong for numbers.

my @names = qw(grace ada linus); my @sorted = sort @names; print "@sorted\n"; # ada grace linus

perl demo.pl

ada grace linus

sort returns a new list and leaves the original alone, which is one more reason to keep it inside an expression. To sort in place, assign the result back to the same array.

Numbers need a block

As strings, 10 comes before 9. Give sort a block that compares with the spaceship operator and the numbers line up.

my @nums = (10, 9, 100); print join(" ", sort @nums), "\n"; # 10 100 9 print join(" ", sort { $a <=> $b } @nums), "\n"; # 9 10 100

perl demo.pl

10 100 9
9 10 100

Inside the block the two values being compared are $a and $b, package variables that sort fills in. Swap them for a descending order: $b <=> $a.

Sort by a field

For records, the block compares the field that matters. A tie-breaker in the same block keeps equal values in a stable order instead of an arbitrary one.

my @users = ( { name => "Ada", age => 36 }, { name => "Grace", age => 45 }, { name => "Alan", age => 41 }, ); my @by_age = sort { $a->{age} <=> $b->{age} } @users; print join(", ", map { $_->{name} } @by_age), "\n"; # Ada, Alan, Grace

perl demo.pl

Ada, Alan, Grace

Sort a hash by value

Hashes have no order, but keys can be sorted by their values. The classic form sorts keys and reads the hash inside the block.

my %hits = (index => 10, about => 2, blog => 7); my @top = sort { $hits{$b} <=> $hits{$a} } keys %hits; print "@top\n"; # index blog about

perl demo.pl

index blog about

Comparing $b with $a reverses the order, so the biggest number comes first. The same trick sorts any hash by any numeric value.

The Schwartzian transform

A sort block runs many times, so an expensive key function is called again and again. Build each key once, sort the pairs, then throw the keys away. The idiom is named after Randal Schwartz.

my @files = qw(b.log a.txt c.md); my @by_ext = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, ($_ =~ /\.(\w+)$/)[0] ] } @files; print "@by_ext\n"; # b.log c.md a.txt

perl demo.pl

b.log c.md a.txt

Read it from the bottom: map builds pairs, sort compares the second slot, map takes the first slot back. For plain string sorts the extra machinery is not worth it, so use it only when the key is expensive.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. How does sort @list compare two elements when no block is given?

    Answer

    As strings, with cmp

    The default comparison is the string one, which is right for names and paths and wrong for numbers.

  2. Why does sort (10, 9, 100) put 10 before 9?

    Answer

    The comparisons are textual, and "1" comes before "9"

    A string comparison walks the characters, so the numeric order needs a block with <=>.

  3. Which variables hold the two values inside a sort block?

    Answer

    $a and $b, package variables that sort fills in

    The block is handed each pair in the package variables $a and $b, which is why the block cannot use my ($a, $b).

  4. What does a tie-breaker in the sort block give you?

    Answer

    A defined order for equal keys, instead of an arbitrary one

    When the first comparison returns 0 the second one decides, so records with the same age keep a stated order.

  5. How do you sort the keys of a hash by their values, biggest first?

    Answer

    sort { $h{$b} <=> $h{$a} } keys %h

    Sorting the keys and reading the hash inside the block compares the values, and $b before $a reverses the order.

  6. What problem does the Schwartzian transform solve?

    Answer

    An expensive key function that would run again for every comparison

    It builds each key once, sorts cheap pairs and drops the keys afterwards, which the last map does.

  7. What does this program print? my @n = (10, 9, 100); print join(" ", sort @n), "\n";

    Answer

    10 100 9

    The default sort compares the elements as text, so the strings line up character by character.

  8. What does this program print? my @n = (10, 9, 100); print join(" ", sort { $a <=> $b } @n), "\n";

    Answer

    9 10 100

    The spaceship operator compares numerically, so the smallest number comes first.

  9. What does this program print? my %h = (a => 2, b => 5); my @k = sort { $h{$b} <=> $h{$a} } keys %h; print "@k\n";

    Answer

    b a

    The values are compared in reverse, so the key with 5 comes before the key with 2.

  10. What does this program print? my @n = (3, 1, 2); print join(",", sort { $b <=> $a } @n), "\n";

    Answer

    3,2,1

    Swapping $a and $b in a numeric comparison sorts from the largest down.

Article author: Arthur Isaev