Alfred Hitchkock
Alfred Hitchkock

Reputation: 365

Groovy: Compare float values and find out greater value

I have a float value and a list of float values where I want to compare the float value with list and find out the greater value than the float value and greater than the whole number of the float value with the list.

Eg:

cv = 1.5

av = [1.1,1.5,1.7,1.9,1.11,2.1,2.5]

Current code :

versions = av.findAll {
def isVersionGreater
it.indexed().any { i, v ->
if (cv[i] == v) return false
isVersionGreater = v > (cv[i] ?: 0)
return true
}
return isVersionGreater
}
versions.removeAll { it[0] > cv[0] }
versions.collect { it.join('.') }

which prints [1.7,1.9,1.11], but I don't want the value with 1, I want to compare and find out only [2.1,2.5] not the other values.

Note: I am collecting the variables av and cv as below


av = output2.tokenize().collect { it.tokenize('.').collect { it as int } } 

cv = output.tokenize().collect { it.tokenize('.').collect { it as int } }.first()

Could someone help me to achieve this using groovy?

Upvotes: 0

Views: 582

Answers (1)

Maicon Mauricio
Maicon Mauricio

Reputation: 3001

Use <= instead of >:

versions.removeAll { it[0] <= cv[0] }
print versions.collect { it.join('.') }

It means remove all from it where its first value is less or equal to first value of cv.

Output:

[2.1, 2.5]

Upvotes: 1

Related Questions