Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Numeric sorting WITHOUT <=>

by AnomalousMonk (Abbot)
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

Comment on Re: Numeric sorting WITHOUT <=>
Download Code
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2015-10-10 16:27 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (257 votes), past polls