I would like to know how can I split my table into subtables of 9's.
Example:
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
Code shall return:
{ {1, 2, 3, 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18}, { 19, 20} }
How do you think is this done?
Your code seems over complicated. The task is to create a subtable every 9 elements. The code below does that:
a={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }
b={}
j=0
k=9
for i=1,#a do
if k==9 then j=j+1; b[j]={}; k=0 end
k=k+1
b[j][k]=a[i]
end
Here, j tracks the number of subtables created and k tracks the number of elements added to a subtable. When k becomes 9, a new subtable is created. k starts at 9 to signal that.
# this code works
list = (0..20).to_a
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
odd = list.select { |x| x.odd? }
# => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
list.reject! { |x| x.odd? }
# => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# but can i emulate this type of functionality with an enumerable method?
set = [1,5,10]
# => [1, 5, 10]
one, five, ten = set
# => [1, 5, 10]
one
# => 1
five
# => 5
ten
# => 10
# ------------------------------------------------
# method I am looking for ?
list = (0..20).to_a
odd, even = list.select_with_reject { |x| x.odd? }
# put the matching items into the first variable
# and the non-matching into the second
Sure, you can do:
odd, even = list.partition &:odd?
odd = []
even = []
list = [1..20]
list.each {|x| x.odd? ? odd << x : even << x }
As pguardiario said, the partition method is the most direct way. You could also use Set#divide:
require 'set'
list = (1..10).to_a
odd, even = Set.new(list).divide(&:odd?).to_a.map{|x| x.to_a}
You could try below:
odd,even = (0..20).group_by(&:odd?).values
p odd,even
Output:
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
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)
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?
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.
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.