ww has asked for the wisdom of the Perl Monks concerning the following question:

I'm walking through the examples and exercises in "Effective Perl Programming" by Joseph N. Hall and Randal Schwartz (2 asides: 1) yeah, Merlyn and, 2) reading on my Kindle, where the formatting is inconsistent and some fairly odd typos mar the text; looks like it was translated to Kindle format by someone unfamiliar with Perl).

This script, pure ASCII -- or so I think, is adapted from an example on sorting:

#!/usr/bin/perl use Modern::Perl; say "\n\n\t sort on multiple keys"; my @first = qw(John Joe John Jon Fred Rich); my @last = qw(Smith Smith Jones Jackson Jones Adler); my @index = sort { $last[$a] cmp $last[$b] or $first[$a] cmp $last[$b] } 0 .. $#first; for (@index) { say "$last[$_], $first[$_]"; } say "\n \t But, testing, single sort (cmp):"; my @sorted = sort { $a cmp $b } qw(John Joe John Jon Fred Rich); for (@sorted) { say "\t $_"; } =head execution sort on multiple keys Adler, Rich Jackson, Jon Jones, John Jones, Fred Smith, John Smith, Joe But, testing, single sort (cmp): Fred Joe John John Jon Rich ## sort puts "John" before "Joe" in the first instance but not in the +second: Why? =cut

Can someone point me to the document I failed to read or the section I failed to read carefully enough that explains the disparity in the output?

Replies are listed 'Best First'.
Re: Variance in sort order (collation)
by tangent (Parson) on Feb 21, 2012 at 02:34 UTC
    There does seem to be a problem in multiple keys sort:
    $last[$a] cmp $last[$b] or $first[$a] cmp $last[$b]
    should be:
    my @index = sort { $last[$a] cmp $last[$b] or $first[$a] cmp $first[$b] } 0 .. $#first; sort on multiple keys Adler, Rich Jackson, Jon Jones, Fred Jones, John Smith, Joe Smith, John But, testing, single sort (cmp): Fred Joe John John Jon Rich
      Bingo! Thank you, tangent!

      and apologies to all the little electrons I inconvenienced by my bleary-eyes ...to say nothing of the Reverend Monks.

Re: Variance in sort order (collation)
by Anonymous Monk on Feb 21, 2012 at 02:12 UTC

    sort puts "Joe" before "John" in the first instance but not in the second: Why?

    Because its not "Joe" and "John", its "Jones, John" and "Smith, Joe"

      Actually no.

      The question relates to

      035: Smith, John 036: Smith, Joe

      versus

      040: Joe 041: John 042: John

        Actually no. The question relates to ...

        How can you tell?