Grails 排序 asList() 、 sort()

标签 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)

最佳答案

您不需要对游戏调用sort(),因为您的条件结果已经排序。

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

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

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

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

关于Grails 排序 asList() 、 sort(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15428487/

相关文章:

eclipse - GGTS未附带任何资料

groovy - 在枚举内的匿名类中声明实例变量

Grails 应用程序根上下文

java - 即使我在正确的域中,Selenium addCookie 也会出现无效的 Cookie 域异常

java - 用于获取事件的 VMWare API

java - chalice 。使用@GrailsCompileStatic时如何处理 'mixed'方法和变量

grails - 如何修复groovy.lang.MissingMethodException:计算总数时方法没有签名

hibernate - 为什么在Grails中使用此Spock规范会导致 'identifier of an instance of projectname.Event was altered from 1 to 2;'?

grails - GGTS 3.1中的Grails单元测试失败,出现BytecodeInterface8的NoClassDefFoundError错误

json - 在Grails中测试CustomObject编码器?