[Dbix-class] natural sort by column

Byron Young Byron.Young at riverbed.com
Fri Jun 13 23:08:59 BST 2008


Hi Emily,

This is untested and inelegant, but might get you closer to what you want:

my @data = $c->model('DB::MyModel')->search(...);

@data = sort { $a->sort_column =~ /^(.*)(\d*)$/;
               my $a_prefix = $1; my $a_suffix = $2;
               $b->sort_column =~ /^(.*)(\d*)$/;
               my $b_prefix = $1; my $b_suffix = $2;
               $a_prefix cmp $b_prefix ||
               $a_suffix <=> $b_suffix } @data;


Byron




From: Emily Heureux [mailto:emily at burnham.org]
Sent: Friday, June 13, 2008 2:47 PM
To: 'DBIx::Class user and developer list'
Subject: [Dbix-class] natural sort by column

Ok, I have tried to accomplish my 'natural' sort in mysql, template toolkit, and in perl to no avail.  I have a result set returned by using search like this:

$rs = $c->model('DB::TableName')->search(...);

I have a 'natural sort' function in perl that takes and returns an array sorted the way a human would sort (john10 would come AFTER john2, etc) but how do I get my $rs or even $col = $rs->get_column('column name') into a hash I can use from within perl?

-Emily






More information about the DBIx-Class mailing list