Jose Hales-Garcia
2006-Oct-31 02:42 UTC
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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Bala Paranj
2006-Oct-31 16:40 UTC
Re: Finding numeric maximum of numbers when they''re stored as strings
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Phrogz
2006-Oct-31 17:02 UTC
Re: Finding numeric maximum of numbers when they''re stored as strings
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---