在SilverStripe 3.1中,我可以通过执行以下操作获取子项的排序列表:
$this->Children()->sort('Title', 'ASC');
但是当我这样做时,大写字母(作为一个组)出现在小写字母之前(作为一个组);因此“D”出现在“a”之前:
Aadb
Bdbdd
Cdbd
Dbddb
aeb
但我想要一个这样的排序顺序:
Aadb
aeb
Bdbdd
Cdbd
Dbddb
我怎么能在SilverStripe中做到这一点?
编辑
我找到了类似的question,威尔说:
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.
有谁知道如何做到这一点?
我尝试了以下但它没有返回任何结果:
function SortedChildren(){
$sortChildren = $this->Children()->map();
natcasesort($sortChildren);
return $sortChildren;
}
解决方法:
好的,最后我想出了如何编写和使用我自己的排序逻辑:
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;
}
然后在模板中使用:
<% loop SortChildren %>
<div class="child"><a href="$ChildURL">$Title</a></div>
<% end_loop %>