2013-03-15 75 views
2

我想要的结果在这里我的代码进行排序:Grails的排序asList(),排序()

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) 
+0

什么是'games.round.sorting'? – 2013-03-15 09:29:15

+0

@ joe69,你做了两次排序:首先在你的查询中:'order'date','asc'',然后在它之后。 – eugene82 2013-03-15 09:32:53

回答

0

从你的代码,目前尚不清楚是什么games.round.sorting一样。

但是看看下面的链接了解更多关于在常规排序列表:
Custom list sorting
Groovy OrderBy

或Google for Groovy Comparator找到其他例子和说明。

1

你并不需要调用gamessort()因您的标准的结果已经排序。

而是按日期排序:order 'date', 'asc',你可以在这里添加你需要的属性。

如果你真的需要你必须通过一个ComparatorClosuresort()结果进行排序:

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

你应该提供一些更多的信息。貌似sortingArrayList - 应该如何games进行排序?

+0

所有游戏都有回合。 2型号: – joe69 2013-03-15 09:46:45

+0

请编辑您的问题。添加您的域并描述您所需的排序标准。 – aiolos 2013-03-15 09:53:19

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的排序方法需要封闭返回排序键,而你试图通过所有排序关键字的列表中的所有游戏。