加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在SilverStripe 3.1中按字母顺序排序(但大写字母不是

发布时间:2020-12-13 22:23:38 所属栏目:PHP教程 来源:网络整理
导读:在SilverStripe 3.1中,我可以通过执行以下操作获取子项的排序列表: $this-Children()-sort('Title','ASC'); 但是当我这样做时,大写字母(作为一个组)出现在小写字母之前(作为一个组);因此“D”出现在“a”之前: Aadb Bdbdd Cdbd Dbddb aeb 但我想要一个这样
在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 %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读