Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Sort a DataObjectSet by a Variable of an ArrayData Object inside it.

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

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

Page: 1
Go to End
Author Topic: 1789 Views
  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Sort a DataObjectSet by a Variable of an ArrayData Object inside it. Link to this post

    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;

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Sort a DataObjectSet by a Variable of an ArrayData Object inside it. Link to this post

    Have you tried using the sort function on the dataobjectset.

    $myDataObjectSet->sort($sortField);

  • Jardiamj
    Avatar
    Community Member
    17 Posts

    Re: Sort a DataObjectSet by a Variable of an ArrayData Object inside it. Link to this post

    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.

    1789 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.