Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Numeric sorting WITHOUT <=>

by AnomalousMonk (Chancellor)
on Oct 09, 2012 at 22:03 UTC ( #998091=note: print w/replies, xml ) Need Help??

in reply to Numeric sorting WITHOUT <=>

Why, oh why would you want to do numeric sorting without the numeric comparison operator? Interview question? Failure of communication? XY Problem? In any event, maybe something like this:

>perl -wMstrict -le "my %jetsons = ( 7 => 'Judy', 10 => 'Jane', 11 => 'George', 2 => 'Elroy', 100=> 'Rosey', 1 => 'Astro', 19 => 'Mr. Spacely', 22 => 'Mr. Cogswell' ); ;; my @sorted = sort non_spaceship keys %jetsons; print qq{@sorted}; ;; sub non_spaceship { return $a < $b ? -1 : $a > $b ? 1 : 0 } " 1 2 7 10 11 19 22 100

Replies are listed 'Best First'.
Re^2: Numeric sorting WITHOUT <=>
by ria1328 (Initiate) on Oct 09, 2012 at 22:22 UTC

    Not my idea, its for a class. That might work but he hinted at using nested loops to numerically sort the keys.

      Ah: "Give a man a fish and he will eat for a day. Tell a man to beat the fish to death with a baseball bat and he will happily use a fishing hook and line for all the rest of his days."

      If he's talking about nested loops, then you're probably not supposed to use the Perl sort function at all, but instead to implement a sorting algorithm from scratch. Hopefully, then, the class has already covered at least one sorting algorithm for you to implement. If not, google can provide explanations of how several sorting algorithms work, then it's just a matter of translating the algorithm's description into Perl code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998091]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2018-06-23 12:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.