carmen-ruby / carmen-rails

NOT ACTIVELY MAINTAINED Rails adapter for Carmen (provides country_select and subregion_select)
MIT License
215 stars 159 forks source link

Sort order in country_select #7

Closed lacco closed 12 years ago

lacco commented 12 years ago

ruby 1.9.3p194 rails 3.2.3 carmen-rails 1.0.0.beta3

I am a little confused about the sort order of the countries generated by country_select. The sorting algorithm from https://github.com/jim/carmen-rails/blob/ada9fc20b65123629fb0831b5997f81f9bfb2a5e/lib/carmen/rails/action_view/form_helper.rb#L88 behaves like this on the command line:

Carmen::World.instance.subregions.sort{|a, b| a.name.unpack('U').to_s <=> b.name.unpack('U').to_s}.map{|r| [r.name, r.name.unpack('U').to_s]}
 => [... ["Greece", "[71]"], ["Germany", "[71]"], ["Guatemala", "[71]"] ... ]

As you can see, the order isn't correct because all countries with "G" have the same sort key "[71]".

lacco commented 12 years ago

I backported my code to carmen 0.2.13, and now the sorting is fine.

jim commented 12 years ago

You're right, this is an issue.

I think the way to go with this is to define <=> on Carmen::Country, and then have the view helper just call sort on the collection. I'll take a stab at this.

rdsoze commented 12 years ago

Removing the unpack('U') puts the countries in the correct sort order. A bit confused here about what unpack('U') does ..

jim commented 12 years ago

Fixed in master.