4

In SilverStripe 3.1 I can get a sorted list of Children by doing the following:

$this->Children()->sort('Title', 'ASC');

But when I do this the capital letters (as a group) come before the lowercase (as a group); thus "D" comes before "a":

Aadb
Bdbdd
Cdbd
Dbddb
aeb

But I want a sort order like this:

Aadb
aeb
Bdbdd
Cdbd
Dbddb

How can I do this in SilverStripe?

EDIT

I found a similar question where Willr says:

Strange! I would have thought it would be case insensitive. You could simply export the array list as an array ($list->map()) then write your own sort logic.

Does anyone know how to do this?

I have tried the following but it does not return any results:

function SortedChildren(){
    $sortChildren = $this->Children()->map();
    natcasesort($sortChildren);
    return $sortChildren;
}
3dgoo
  • 15,716
  • 6
  • 46
  • 58
iraira
  • 315
  • 2
  • 13

1 Answers1

0

Ok, finally I figured out how to write and use my own sort logic:

function SortChildren() {

     $_list = $this->Children()->map("URLSegment", "Title");
     natcasesort($_list);

     $sortedChildren = new ArrayList();
     foreach($_list as $key => $value ){
         $fields = new ArrayData(array('ChildURL' => $key, 'Title' => $value));
         $sortedChildren->push($fields);
     }  
     return $sortedChildren;

}

And then use in template:

<% loop SortChildren %>
      <div class="child"><a href="$ChildURL">$Title</a></div>
<% end_loop %>
Turnerj
  • 4,258
  • 5
  • 35
  • 52
iraira
  • 315
  • 2
  • 13