Grails排序asList(),sort()

时间:2013-03-15 09:14:34

标签: grails groovy

我想在此处对结果进行排序:

    def games = Game.withCriteria() { 
                    eq('season', currentSeason) 
                    eq('competition', competition) 
                    round { 
                            ge('roundNr', startRound.roundNr) 
                    } 
                    or { 
                            round { 
                                    le('roundNr', currentRound.roundNr) 
                            } 
                            gt('state', Game.STATE_NOT_STARTED) 
                    } 
                    order 'date', 'asc' 
            }     

      def sortGames = games.asList().sort(games.round.sorting) 

查询工作正常,我想对它们进行排序。

错误:

   Error 500: Executing action [recalcCompetitionTable] of controller [at.ligaportal.CompetitionController] caused exception: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size() 
    Servlet: grails 
    URI: /ligaportal/grails/competition/recalcCompetitionTable.dispatch 
    Exception Message: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size() 
    Caused by: No signature of method: java.util.ArrayList.sort() is applicable for argument types: (java.util.ArrayList) values: [[1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16]] Possible solutions: sort(), sort(java.util.Comparator), sort(groovy.lang.Closure), wait(), size(), size() 
    Class: ApiAuthenticationFilter 
    At Line: [77] 

我的问题在哪里?

我通过withCriteria()方法获得了游戏,按日期排序。

所有游戏都有轮次,现在我想通过轮次排序对它们进行排序。

5场比赛有1轮比赛,而比赛有比赛日期。

Round 1 = 18.09.2012 - 25.09.2012 (5 Games)
Round 2 = 26.09.2012 - 31.09.2012 (4 Games) and one Game plays later 
5.11.2012
Round 3 = 1.10.2012 - 7.10.2013

And my problem is, i sum up the round with a each and previous round.

Roudn 1 = 3,3,3,1,1 Points
Round 2 = 6,4,4,2,1 Points -> 1 Games later 
Round 3 = 9,7,7,4,3 Pounts
Round 2 = 6,4,4,4,1 (after game)
Round 4 = here i cant go back to round 4 -> the one game is lost!

现在我想通过game.round.sorting

订购游戏

这是我的问题。

谢谢!

解决方案

def bySorting = new Comparator() {
        int compare(a,b) { a.round.sorting <=> b.round.sorting }
    }
    games.sort(bySorting)

3 个答案:

答案 0 :(得分:1)

由于您的条件结果已经排序,您无需在sort()上致电games

相反按日期排序:order 'date', 'asc'您可以在此处添加所需的属性。

如果您确实需要对结果进行排序,则必须将ComparatorClosure传递给sort()

def sortGames = games.asList().sort{it.round.sorting}

您应该提供更多信息。看起来sortingArrayList - games应如何排序?

答案 1 :(得分:1)

您应该能够在标准中使用createAlias作为初始数据库查询的一部分进行排序:

def games = Game.withCriteria() {
                createAlias('round', 'rnd')

                eq('season', currentSeason) 
                eq('competition', competition) 
                ge('rnd.roundNr', startRound.roundNr) 
                or { 
                        le('rnd.roundNr', currentRound.roundNr) 
                        gt('state', Game.STATE_NOT_STARTED) 
                } 
                order 'rnd.sorting', 'asc' 
        }

这样您就不需要在Groovy中对返回的列表进行任何排序。


但是,如果您确实希望按照round.sorting值对现有游戏列表进行排序,那么您几乎拥有正确的语法。你真正需要的是:

def sortGames = games.sort { it.round.sorting }

Groovy排序方法需要一个返回排序键的闭包,而你的尝试是传递所有游戏的所有排序键的列表。

答案 2 :(得分:0)

从您的代码中不清楚games.round.sorting的作用。

但请查看以下链接,了解有关在groovy中排序列表的更多信息:
Custom list sorting
Groovy OrderBy

或谷歌Groovy Comparator查找其他示例和说明。