joe69
joe69

Reputation: 97

Grails sorting asList() , sort()

I want to sort the result here my code:

    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) 

The query works fine, i want to sort them.

Error:

   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] 

where is my Problem?

I got the Games via withCriteria() method, sort by date.

And all games have rounds , and now i want to sort them by the sorting of the rounds.

5 Games have 1 Round and alle Games have dates.

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!

And now i want to order the games via game.round.sorting

This is my problem.

Thank you!

The Solution

def bySorting = new Comparator() {
        int compare(a,b) { a.round.sorting <=> b.round.sorting }
    }
    games.sort(bySorting)

Upvotes: 2

Views: 4671

Answers (3)

Ian Roberts
Ian Roberts

Reputation: 122364

You should be able to sort by round as part of the initial database query by using createAlias in your criteria:

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' 
        }

This way you don't need to do any sorting of the returned list in Groovy.


However, if you do want to sort an existing list of games by their round.sorting value then you almost had the right syntax. What you actually need is:

def sortGames = games.sort { it.round.sorting }

The Groovy sort method expects a closure that returns the sort key, whereas your attempt was passing a list of all the sort keys for all the games.

Upvotes: 1

aiolos
aiolos

Reputation: 4697

You don't need to call sort()on games due to the result of your criteria is already sorted.

Instead sorting by date : order 'date', 'asc' you could add your desired property here.

If you really need to sort the result you have to pass an Comparator or a Closure to sort():

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

You should provide some more information. Looks like sorting is an ArrayList - how should the games be sorted?

Upvotes: 1

moeTi
moeTi

Reputation: 3904

From your code it is not clear what games.round.sorting does.

But have a look at the following links to learn more about sorting lists in groovy:
Custom list sorting
Groovy OrderBy

Or google for Groovy Comparator to find other examples and descriptions.

Upvotes: 0

Related Questions