Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Sort a DataObjectSet by a Variable of an ArrayData Object inside it.


Go to End


3 Posts   2642 Views

Avatar
Jardiamj

Community Member, 17 Posts

14 March 2009 at 9:51am

Edited: 17/03/2009 12:22pm

Hello everyone!, my problem is as follow: I'm working with several xml files that contain information about plants. I'm getting the contents from the files, then I pass it to an array and after to a DataObjectSet that I can access in my template. So far so good.
Now my problem is that I want to sort the information by an attribute that I defined in my xml files called sort. My array looks like this:

Array ( [0] => Array ( [name] => common [attributes] => Array ( [caption] => Common Name [sort] => 1 ) [text] => Concolor Fir [children] => Array ( ) ) [1] => Array ( [name] => latin [attributes] => Array ( [caption] => Latin Name [sort] => 2 ) [text] => Abies concolor [children] => Array ( ) )

And my DataObjectSet look like this:

DataObjectSet Object ( [items:protected] => Array ( [0] => ArrayData Object ( [array:protected] => Array ( [name] => common [attributes] => Array ( [caption] => Common Name [sort] => 1 ) [text] => Concolor Fir [children] => Array ( ) ) [iteratorPos:protected] => [iteratorTotalItems:protected] => [failover:protected] => [_object_cache:protected] => Array ( ) [_xml_cache:protected] => Array ( ) [_natural_cache:protected] => Array ( ) [customisedObj:protected] => [parent:protected] => [namedAs:protected] => [extension_instances:protected] => Array ( ) [class] => ) [1] => ArrayData Object ( [array:protected] => Array ( [name] => latin [attributes] => Array ( [caption] => Latin Name [sort] => 2 ) [text] => Abies concolor [children] => Array ( ) ) [iteratorPos:protected] => [iteratorTotalItems:protected] => [failover:protected] => [_object_cache:protected] => Array ( ) [_xml_cache:protected] => Array ( ) [_natural_cache:protected] => Array ( ) [customisedObj:protected] => [parent:protected] => [namedAs:protected] => [extension_instances:protected] => Array ( ) [class] => )

I have tried without any success to sort the array before doing DataObjectSet->push($array). I tried to use the usort function, but haven't figured it out so far. Now I'm wondering if there is a function that would allow me to sort the information in my DataObjectSet by the value in $attributes.sort. Or if I could build my own, how could I start with it?

Thanks;

Avatar
Willr

Forum Moderator, 5523 Posts

15 March 2009 at 2:41pm

Have you tried using the sort function on the dataobjectset.

$myDataObjectSet->sort($sortField);

Avatar
Jardiamj

Community Member, 17 Posts

17 March 2009 at 8:00am

Hello Willr!

Yes I have tried it but as you an see my sortField is not really a field inside the dataobjectset but a field inside an array inside a ArrayData Object of my DataObjectSet, so I don't if I can reference it in the sort function and if I can do so, I don't know how.

DataObjectSet Object ( [items:protected] => Array ( [0] => ArrayData Object ( [array:protected] => Array ( [name] => common [attributes] => Array ( [caption] => Common Name [sort] => 1 ) [text] => Fraser Fir [children] => Array ( ) ) [iteratorPos:protected] => [iteratorTotalItems:protected] => [failover:protected] => [_object_cache:protected] => Array ( ) [_xml_cache:protected] => Array ( ) [_natural_cache:protected] => Array ( ) [customisedObj:protected] => [parent:protected] => [namedAs:protected] => [extension_instances:protected] => Array ( ) [class] => )

In my template I can use $attributes.caption and $attributes.sort inside the control block. So I tried:
$myDataObjectSet->sort($attributes.sort);

It doesn't work. So I don't know how to do it. I haven't found the way to sort the array before passing it into a DataObjectSet without modifying it either.
Some suggestion will be really appreciated.

Thanks.