Finding numeric maximum of numbers when they're stored as strings

I have a field used to hold the number of a publication's volume series. The field is defined as type string, since in most cases it's used as a string and I'm not doing any calculations with it.

How can I find the numerical maximum of the field and not the alphabetic maximum? I want...

3 < 17

and not...

"17" < "3"

I can convert the field to be type integer, but I wondered if there's a trick with the maximum method that I could use. I tried passing 'CAST(number AS UNSIGNED)' to the conditions option. But it didn't change the result.

Thanks in advance, Jose

You have to convert the String object to integer by using to_i method. If the values are stored in an array, you can do : [2,5,8].max to get the maximum value. So the answer is that there is no trick that can compare without converting it to an integer.

Jose Hales-Garcia wrote:

I have a field used to hold the number of a publication's volume series. The field is defined as type string, since in most cases it's used as a string and I'm not doing any calculations with it.

p [ "17", "3" ].max #=> 3

p [ "17", "3" ].map{ |s| s.to_i }.max #=> 17