可以在Velocity中按日期对列表进行排序吗?

问题描述

我已经找到了用于按标签获取文章并将其显示为xWiki中带有链接的列表的代码,但是我希望它按日期进行排序。

有人对我有建议吗?

  {{veLocity}}  
    #set ($list = $xwiki.tag.getDocumentsWithTag('mytag'))
      #foreach($reference in $list)
    #set ($document = $xwiki.getDocument($reference))
    #set ($label = $document.getTitle())
      [[$label>>$reference]]
    #end
  {{/veLocity}}

谢谢!

解决方法

对速度进行排序可能会导致以下两种性能损失之一:

  1. 使用排序算法->不必要地复杂地实际上进行速度排序
  2. 将所有文档结果加载到内存(一个集合)中,然后 使用sort/collection tool->对集合进行排序,如果结果比预期的要大,您可能会很快耗尽内存。

考虑到XWiki在其后运行,最简单的选择是对存储在文档中的XWiki.TagClass对象进行XWQL查询,并在数据库级别进行排序。此时,就速度而言,您只需要显示结果:

{{velocity}}
#foreach ($docStringRef in $services.query.xwql("from doc.object(XWiki.TagClass) tagsObj where 'conference' member of tagsObj.tags order by doc.creationDate DESC").setLimit(10).execute())
  #set ($document = $xwiki.getDocument($docStringRef))
  [[$document.title>>$docStringRef]]
#end
{{/velocity}}

为了将来使用/参考,XWiki中可用的Velocity工具列表也可能会有用https://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity%20Module#HVelocityTools,因为它们可以帮助进行常见的操作,包括排序(我在上面的第2点提到)