Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Numeric sorting WITHOUT <=>

by AnomalousMonk (Canon)
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998091]
help
Chatterbox?
[dazz]: Hi I have grabbed an image from an IP camera with Image::Grab. I then want to pass that image object to a subroutine where I do a read-only test on the image with Image:Magick.
[dazz]: How do I pass an image object to a subroutine and then apply a Image::Magick a read-only method to the image object???
[dazz]: Also, I want to use Image::Magick to annotate and composite (not in a sub). How do I take a Image::Grab object and apply Image::Magick write methods to it??
[dazz]: At present, I am saving the Image::Grab image to disk, then creating a new Image::Magick object that reads the disk file.
[dazz]: .
[choroba]: Discipulus Sounds interesting. Any pictures of the puppets or paintings?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-03-27 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (317 votes). Check out past polls.