[Dbix-class] natural sort by column

Byron Young Byron.Young at riverbed.com
Fri Jun 13 23:42:24 BST 2008


Just do what I did to get the @data array, or call all() on a resultset.  Read the DBIx::Class::ResultSet documentation -- it says that search() returns an array of object in list context and a resultset in scalar context.  Once you have your objects in an array, you can manipulate them just like you would anything else in perl.  It's just a list of row objects; there's no magic there.

byron


Emily Heureux wrote on 2008-06-13:
> I guess my general question is, what do you guys do when you need to
> manipulate your resultsets from within perl (or whatever)?
>
> E
>
>> -----Original Message-----
>> From: Emily Heureux [mailto:emily at burnham.org]
>> Sent: Friday, June 13, 2008 3:23 PM
>> To: 'DBIx::Class user and developer list'
>> Subject: RE: [Dbix-class] natural sort by column
>>
>> Hi Byron.  I have the sorting algorithm that works on arrays at this
>> point. My question is even more basic.  How do I create a @data (or
>> even a %data) like you have below, from a result set?
>>
>> Emily
>>
>>> -----Original Message-----
>>> From: Byron Young [mailto:Byron.Young at riverbed.com]
>>> Sent: Friday, June 13, 2008 3:09 PM
>>> To: DBIx::Class user and developer list
>>> Subject: RE: [Dbix-class] natural sort by column
>>>
>>> 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
>>>
>>>
>>>
>>>
>>> _______________________________________________ List:
>>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix- class IRC:
>>> irc.perl.org#dbix-class SVN:
>>> http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable
>>> Archive: http://www.grokbase.com/group/dbix- class at lists.rawmode.org
>>
>>
>>
>> _______________________________________________ List:
>> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix- class IRC:
>> irc.perl.org#dbix-class SVN:
>> http://dev.catalyst.perl.org/repos/bast/DBIx-Class/ Searchable Archive:
>> http://www.grokbase.com/group/dbix- class at lists.rawmode.org
>
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-
> class at lists.rawmode.org





More information about the DBIx-Class mailing list