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'、ここに目的のプロパティを追加できます。

本当に結果をソートする必要がある場合は、ComparatorまたはClosureに渡す必要がありsort()ます。

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 の sort メソッドは、ソート キーを返すクロージャーを期待していますが、あなたの試行では、すべてのゲームのすべてのソート キーのリストを渡していました。

于 2013-03-15T12:10:00.290 に答える
0

あなたのコードからは、何が何をするのか明確ではありませんgames.round.sorting

ただし、次のリンクを参照して、groovy でのリストの並べ替えの詳細を確認してください:
カスタム リストの並べ替え
Groovy OrderBy

または、グーグルでGroovy Comparator他の例や説明を見つけてください。

于 2013-03-15T09:23:06.333 に答える