There's more than one way to do things PerlMonks

### Re: Numeric sorting WITHOUT <=>

by ikegami (Pope)
 on Oct 10, 2012 at 01:55 UTC ( #998113=note: print w/replies, xml ) Need Help??

in reply to Numeric sorting WITHOUT <=>

```my (\$min) = keys(%jetsons);
my \$max = \$min;
for (keys(%jetsons)) {
\$max = \$_ if \$_ > \$max;
\$min = \$_ if \$_ < \$min;
}

my @sorted;
for (\$min..\$max) {
push @sorted, \$_
if exists(\$jetsons{\$_});
}

Replies are listed 'Best First'.
Re^2: Numeric sorting WITHOUT <=>
by Marshall (Abbot) on Oct 10, 2012 at 04:42 UTC
my (\$min) = keys(%jetsons); This is the number of keys in %jetsons.
This is not the min.
my (\$min) = keys(%jetsons); This is the number of keys in %jetsons.

Actually, it's a randomly (insofar as the hashing algorithm is random) chosen key from the hash. The assignment is in list context and seeds the min/max finder loop that follows.

```>perl -wMstrict -le
"my %jetsons = qw(aa 1  bb 2  cc 3  dd 4  ee 5);
my (\$min) = keys %jetsons;
print \$min;
"
cc

Update: ikegami's code works. (Never doubt a Pope.)

No.
```# keys in scalar context: Number of keys.
my \$min = keys(%jetsons);

# keys in list context: List of keys. First one is assigned
my (\$min) = keys(%jetsons);
Correct.
My mistake.
Re^2: Numeric sorting WITHOUT <=>
by ria1328 (Initiate) on Oct 10, 2012 at 17:07 UTC
Thank you! This worked and it actually made it easier for me to understand how it was done instead of using the was my professor said we should!

This worked and it actually made it easier for me to understand how it was done

My code does NOT demonstrate how sorting is usually done. It takes a different and silly approach in response to your equally silly requirement. It would take forever for my solution to sort

```my %jetsons = ( 0 => "a", 4000000000 => "b" );

Create A New User
Node Status?
node history
Node Type: note [id://998113]
help
Chatterbox?
 [markong]: is there any mean to get notified if there are new replies in a thread? [LanX]: [LanX]: I think there are RSS feeds you could use, but never used them

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-03-19 09:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (238 votes). Check out past polls.

Notices?