[sf-perl] Happy fun sorting problem
Bill Moseley
moseley at hank.org
Fri Jun 10 14:34:58 PDT 2005
On Fri, Jun 10, 2005 at 09:36:51AM -0700, Josh Berkus wrote:
> Folks,
>
> I can think of a few ways to solve this issue, but none that are elegant. I'm
> looking for elegant.
>
> I have a 3-level heirarchy:
>
> Category
> Sub Category
> Item = Value
>
> I want to print output that formats like:
>
> Category-1
> Sub Category-H
> Item_a = Value
> Item_f = Value
> Sub Category-S
> Item_d = Value
> Category-2
> Sub Category-B
> Item_k = Value
> ...etc...
here's some very old code I used in a project once. It builds a single string sort key
for fast sorting. Not sure what all the hash lookups are, but you can
use your imagination. I think I'm missing some bounds checking,
too...
for my $sortby ( @sortby ) {
my $reverse = $sortby eq lc $sortby;
my $key;
my $numeric;
for ( lc $sortby ) {
# General text
if ( $strings{$_} ) {
$key = lc $student->{ $strings{$_} };
last;
}
/date/ && do {
$key = $student->{S_UNIX_REGISTER_TIME} || 0;
$numeric++;
last;
};
/status/ && do {
$key = $status_sort[$student->{S_STATUS_CODE}];
$numeric++;
last;
};
# here's the default
$key = lc "$student->{LAST_NAME}$student->{FIRST_NAME}";
}
if ( $numeric ) {
$key = $key ^ ( 1 << 31 ); # bias to unsigned
$sort_key .= pack 'N', $reverse
? $key ^ 0xffffffff
: $key;
} else {
$key ^= "\xFF" x 150 if $reverse;
$sort_key .= "$key\0";
}
}
$sort_hash{$student} = $sort_key;
}
return [ sort { $sort_hash{$a} cmp $sort_hash{$b} } @{$students} ];
}
--
Bill Moseley
moseley at hank.org
More information about the SanFrancisco-pm
mailing list