Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion The Sort Problem
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rod Adams  
View profile  
 More options Feb 14 2004, 5:48 pm
Newsgroups: perl.perl6.language
From: r...@rodadams.net (Rod Adams)
Date: Sat, 14 Feb 2004 16:30:34 -0600
Local: Sat, Feb 14 2004 5:30 pm
Subject: Re: The Sort Problem

Austin Hastings wrote:
>Off the top of my head, I can't think of a case where the compare sub
>would be needed unless the key was not well-ordered. Does anyone have
>an example of a case where the key-extraction sub approach doesn't
>reduce the problem to a Scalar comparison?

I can't find the P5 code I used for it right off, but I remember a case
when I was playing around with various football (US) stats. I was
ranking teams, and had something akin to:

@teams = sort {$b->{WinPct} <=> $a->{WinPct}  ||
               ($a->{Conference} eq $b->{Conference}
                ?($b->{ConfWinPct} <=> $a->{ConfWinPct} ||
                  $b->{ConfWon}    <=> $a->{ConfWon}   ||
                  $a->{ConfLost}   <=> $b->{ConfLost})
                :($a->{Division} eq $b->{Division}
                  ?($b->{DivWinPct} <=> $a->{DivWinPct} ||
                    $b->{DivWon}    <=> $a->{DivWon}   ||
                    $a->{DivLost}   <=> $b->{DivLost})
                  :0
                  )
                ) ||
               $b->{Won}         <=> $a->{Won}  ||
               $a->{Lost}        <=> $b->{Lost} ||
               $b->{GamesPlayed} <=> $a->{GamesPlayed}
              } @teams;

Creating a keycode for this situation is not a trivial task at all. So
sorts do occur in the real world for which there is no straightforward
way to generate a sortkey. Albeit considerably rare.

Also, I think there is utility in have a compare sub supported so that:
1) porting P5 code is easier. (a minor design rationale, but it exists)
2) people used to thinking in terms of compare subs (from C, P5, and
points of the programming universe) can still think that way.
3) most importantly to me, so that There's More Than One Way to Do It.

-- Rod Adams


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.