# Finding numeric maximum of numbers when they're stored as st

**URL:** <https://www.ruby-forum.com/t/finding-numeric-maximum-of-numbers-when-theyre-stored-as-st/74553>\
**Category:** Rails\
**Created:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/finding-numeric-maximum-of-numbers-when-theyre-stored-as-st/74553 "2006-11-07T10:48:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [November 7, 2006, 10:48am UTC](https://www.ruby-forum.com/t/finding-numeric-maximum-of-numbers-when-theyre-stored-as-st/74553/1 "2006-11-07T10:48:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/finding-numeric-maximum-of-numbers-when-theyre-stored-as-st/74553/2 "2006-11-07T10:49:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [November 7, 2006, 10:49am UTC](https://www.ruby-forum.com/t/finding-numeric-maximum-of-numbers-when-theyre-stored-as-st/74553/3 "2006-11-07T10:49:28Z")

</div>

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
