2

我想在这里对结果进行排序我的代码:

    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)
4

3 回答 3

1

由于您的标准结果已经排序,因此您不需要调用sort()games

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

如果您确实需要对结果进行排序,则必须将 anComparator或 a传递Closuresort()

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

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

于 2013-03-15T09:29:42.883 回答
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 排序方法需要一个返回排序键的闭包,而您的尝试是传递所有游戏的所有排序键的列表。

于 2013-03-15T12:10:00.290 回答
0

从您的代码中不清楚是什么games.round.sorting

但是请查看以下链接以了解有关在 groovy 中排序列表的更多信息:
自定义列表排序
Groovy OrderBy

或谷歌Groovy Comparator查找其他示例和描述。

于 2013-03-15T09:23:06.333 回答