Groovy / Grails:如何按ID排序对象列表 [英] Groovy/Grails : How to sort the list of objects by id

查看:91
本文介绍了Groovy / Grails:如何按ID排序对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PublicTraining Class

PublicTraining Class

class PublicTraining{
    static hasMany = [trainingOrder: TrainingOrder]
}

和TrainingOrder Class

and TrainingOrder Class

class TrainingOrder {
    Date createdOn

    static mapping = {
        sort id:"asc"
    }
}

如果我想获得所有培训订单

if i want to get all the orders for training

def orders = publicTrainingInstance.trainingOrder.sort()
println orders // [59,58] (id of orders)

不会给出排序顺序

推荐答案

默认 sort()对于 Comparable 对象很有用。如果您的课程不是 Comparable ,请使用:

Default sort() is useful for Comparable object. If your class is not a Comparable, use:

def orders = publicTrainingInstance.trainingOrder.sort { it.id }

该代码将使用传入的ID进行排序。

That code will sort by using passed id.

查看文档: http://groovy.codehaus.org/groovy-jdk/java/util/Collection.html#sort()

这篇关于Groovy / Grails:如何按ID排序对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆