2013-03-15 76 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的排序方法需要封閉返回排序鍵,而你試圖通過所有排序關鍵字的列表中的所有遊戲。