0

I have a model called Products and in that model I have a column called "Size". How can I order sort my products by their size? (Something like 9, 9.5, 10, 10.5, 11).

Thanks.

chrisbedoya
  • 820
  • 7
  • 22
  • Hopefully [Here][1] It answers your question. [1]: http://stackoverflow.com/questions/7726968/sort-an-array-of-strings-by-their-integer-values –  Jun 06 '15 at 23:27

1 Answers1

2

If you have to have the field as a string, you can do

Product.all.sort_by{|p| p.size.to_f}

However, if you can change the field from a string to a float you can do the more efficient..

Product.order(:size)
SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53