我如何在 SilverStripe 3.1 中按字母顺序排序(但大写字母不是小写字母之前的一组)?
How do I sort something alphabetically (but the capital letters not as a group before the lowercase) in SilverStripe 3.1?
在 SilverStripe 3.1 中,我可以通过执行以下操作获得 Children
的排序列表:
$this->Children()->sort('Title', 'ASC');
但是当我这样做时,大写字母(作为一个组)排在小写字母(作为一个组)之前;因此 "D" 出现在 "a":
之前
Aadb
Bdbdd
Cdbd
Dbddb
aeb
但我想要这样的排序顺序:
Aadb
aeb
Bdbdd
Cdbd
Dbddb
我如何在 SilverStripe 中执行此操作?
编辑
我发现了一个类似的 question,其中 Willr 说:
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.
有人知道怎么做吗?
我尝试了以下方法,但没有 return 任何结果:
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 %>
在 SilverStripe 3.1 中,我可以通过执行以下操作获得 Children
的排序列表:
$this->Children()->sort('Title', 'ASC');
但是当我这样做时,大写字母(作为一个组)排在小写字母(作为一个组)之前;因此 "D" 出现在 "a":
之前Aadb
Bdbdd
Cdbd
Dbddb
aeb
但我想要这样的排序顺序:
Aadb
aeb
Bdbdd
Cdbd
Dbddb
我如何在 SilverStripe 中执行此操作?
编辑
我发现了一个类似的 question,其中 Willr 说:
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.
有人知道怎么做吗?
我尝试了以下方法,但没有 return 任何结果:
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 %>