【问题标题】:Sort Tool with Custom Objects by date按日期排序带有自定义对象的工具
【发布时间】:2016-10-18 01:44:55
【问题描述】:

我有一个速度中的 TestRecord 对象列表,其中有一个方法 getDateExecuted,它返回一个 java.util.date 对象。我想使用排序工具按日期对记录进行排序。(或适当的排序算法)但是我没有看到任何不使用 xPath 节点作为列表参数的排序工具实现。如果无法使用排序工具,任何有关仅使用 foreach 循环创建冒泡/选择排序的信息都会有所帮助。

排序工具文档: http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/SortTool.html

http://www.hannonhill.com/kb/Script-Formats/#sort-tool

【问题讨论】:

    标签: sorting object arraylist velocity selection-sort


    【解决方案1】:

    将 SortTool 对象添加到上下文中:

    context.put("sortTool", new SortTool());
    

    在模板中,可以得到排序后的列表(按dateExecuted排序)为:

    #foreach($testRecord in $sortTool.sort($testRecordList, "dateExecuted:asc"))
        $testRecord.dateExecuted
    #end
    

    SortTool 文档中有明确规定
    http://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/SortTool.html

    如有任何问题,请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 2012-02-06
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      相关资源
      最近更新 更多