We don't bite newbies here... much PerlMonks

### 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.

Create A New User
Node Status?
node history
Node Type: note [id://998091]
help
Chatterbox?
and the sunlight beams...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2018-04-21 01:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (81 votes). Check out past polls.

Notices?