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

Sorting Criteria

by wizard341 (Acolyte)
 on Aug 05, 2003 at 17:58 UTC Need Help??

wizard341 has asked for the wisdom of the Perl Monks concerning the following question:

Im trying to sort a hash (largest first,then down to smallest) into an array based on some criteria ive specified. I am unable to determine if my sorting method is wrong or if the way im working with it is wrong, so i figure i better ask someone else.
```%reversed = reverse %hash;
@sorted = sort {sorter(\$a, \$b)} keys %reversed;

foreach \$key(@sorted)
{

print \$key;

print
\$co->a
(
{-href => "/References/\$reversed{\$key}"},\$reversed{\$key}
),
\$co->p;

}
thats pretty much how i call the subroutine and what i am doing with it
```sub sorter
{
\$a = shift;
\$b = shift;

if(\$hashYear{\$a} < \$hashYear{\$b})
{
return \$a;
}
elsif(\$hashYear{\$a} == \$hashYear{\$b})
{
if(\$hashMonth{\$a} < \$hashMonth{\$b})
{
return \$a;
}
elsif(\$hashMonth{\$a} == \$hashMonth{\$b})
{
if(\$hashDay{\$a} < \$hashDay{\$b})
{
return \$a;
}
}
}
else
{
#b is greater than a (or, in a rare case the are completly equal)
return \$b;
}

}
And this is my sorting criteria. hashYear,month,and day are just small numbers that kind of sort out which is the newest (based on which is larger). Does anyone see anything that jumps out at them as being wrong?

Replies are listed 'Best First'.
Re: Sorting Criteria
by Aristotle (Chancellor) on Aug 05, 2003 at 18:04 UTC
1. You don't return the elements from the sort block, you return -1, 1 or 0 depending on whether \$a or \$b was greater or they were equal. See perldoc -f sort.

2. You don't need to pass \$a / \$b explicitly that way, they're special globals.

3. In fact, you even shouldn't, since \$a / \$b are aliases, and you're reassigning to them a copy of their value at the top of your block. Alternatively, assign them to lexicals named differently from \$a / \$b. In your case, I'd just drop the shifts, call the function by_date and then say

```@sorted = sort &by_date keys %reversed;

Makeshifts last the longest.

Re: Sorting Criteria
by Zaxo (Archbishop) on Aug 05, 2003 at 18:24 UTC

The way to sort first by year, then month, then day,

```sort {
\$hashYear{\$a} <=> \$hashYear{\$b}
or \$hashMonth{\$a} <=> \$hashMonth{\$b}
or \$hashDay{\$a} <=> \$hashDay{\$a}
} @unsorted;
though I haven't seen what the structure of your elements is. That might indicate another approach such as a Schwarzian Transfornm.

After Compline,
Zaxo

Thanks alot guys, works perfectly now.
Re: Sorting Criteria
by antirice (Priest) on Aug 05, 2003 at 18:12 UTC

Aristotle++ has already answered your questions. However, I would like to point out that your code does not return anything when \$hashMonth{\$a} > \$hashMonth{\$b} -or- \$hashMonth{\$a} == \$hashMonth{\$b} and \$hashDay{\$a} >= \$hashDay{\$b}. You can fix this by removing the else block and just replacing it with return \$b.

antirice
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Sorting Criteria
by Cine (Friar) on Aug 05, 2003 at 18:09 UTC
Aristotle already told what was wrong, so here is a suggestion for your sort method:
```sub sorter {
"\$hashYear{\$a}\$hashMonth{\$a}\$hashDay{\$a}" <=> "\$hashYear{\$b}\$hashMon
+th{\$b}\$hashDay{\$b}"
}

T I M T O W T D I
That will work if days and months are zero-padded. Otherwise, "1/11" will compare equal to "11/1".
Yeah, silly me
```sub sorter {
sprintf"%04d%02d%02d",\$hashYear{\$a},\$hashMonth{\$a},\$hashDay{\$a} <=>
sprintf"%04d%02d%02d",\$hashYear{\$b},\$hashMonth{\$b},\$hashDay{\$b}
}

T I M T O W T D I

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://281108]
Approved by Cine
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2023-12-05 12:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What's your preferred 'use VERSION' for new CPAN modules in 2023?

Results (27 votes). Check out past polls.

Notices?