in reply to Sorting on Exists
(untested, but I think it's the right idea)sort { exists $b->{vtype_ug} <=> exists $a->{vtype_ug} }
A sort sub should return positive if $a is greater than $b, 0 if they are equal, and negative if $b is greater than $a. So if you want items with vtype_ug to always sort before items that don't have it, you need to return -1 if you find it only in $a, 1 if you find it only in $b, or 0 if it is in both or neither.
According to sort's documentation, in Perl 5.7 and newer sorts are "stable", which means that if two items are equal they'll stay in the same order. This should ensure that two sorts work. However, as others have mentioned, you can expect better performance and better portability if you use one sort sub that does both comparisons:
sort { (exists $b->{vtype_ug} <=> exists $a->{vtype_ug}) || ($a->{total_rate} <=> $b->{total_rate}) }
Update: bart is right on all points, and I've corrected them above. Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting on Exists
by bart (Canon) on Sep 09, 2006 at 20:18 UTC |