# fetch data from sql
while (my $row = $res->fetchrow_hashref()) {
my %rowdata;
# $score is generated by some badass algoritms, and is an integer
$rowdata{SCORE} = $score;
$rowdata{ID} = $row->{id};
$rowdata{TITLE} = $row->{title};
push (@output, \%rowdata);
}
my @sorted_rows = sort { $b->{SCORE} <=> $a->{SCORE} } @output;
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - [flyingmoose]
I shouldn't have to say this, but any code, unless otherwise stated, is untested
Here,
@output = sort {$b->{SCORE} <=> $a->{SCORE}} @output;
Each element of @output is a reference to a hash, and it can be dereferenced to get its data. Placing $b on the left of the comparison makes the sort descending.
After Compline,
Zaxo
thor
Oh - and sorting in Perl can occasionally be more efficient than sorting in the database. The reason is that, depending on the implementation, the database will sort by copying the values from one section of memory to another. Perl sorts by moving pointers around. With larger datasets, Perl's way could be faster.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
I shouldn't have to say this, but any code, unless otherwise stated, is untested
perlmonks.org content © perlmonks.org and Anonymous Monk, dragonchild, thor, Zaxo
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03