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.

Data Model Questions /

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

ArrayList Sort not working


Go to End


2 Posts   4036 Views

Avatar
davidglassford

Community Member, 1 Post

30 October 2013 at 2:15am

Hey all,

I'm new to silverstripe and trying to combine two dataobjects into an ArrayList and then sort it by Date/Created but It doesn't do the sort. I've included the code below.

$displayContent = ArrayList::create();

$projectPages = ProjectPage::get()->sort('Created', 'DESC');
$blogPages = BlogEntry::get()->sort('Created');

foreach ($projectPages as $projectPage) {
$displayContent->merge($projectPage);
}
foreach ($blogPages as $blogPage) {
$displayContent->merge($blogPage);
}

$displayContent->sort('Created');

return $displayContent;

Thanks for any help

David

Avatar
Willr

Forum Moderator, 5523 Posts

30 October 2013 at 7:02pm

Lists are mostly immutable (i.e operations on lists give you back new lists) so when you do your sort you need to get your list

$sorted = $list->sort('Created');