I want to pull data from the database once, but do multiple sorts on different columns. I''m pulling a list of products from the database. In my view, I want to create two select lists: one sorted by the product name; one sorted by part number. I know I can map the values into a new array or use "options_from_collection_for_select," but I''m having a little trouble with sorting.>> products.map { |p| [p.id, p.name] }.sort=> [[7, "DataCollector"], [8, "DataTranslator"], [9, "DataMonitor"], [10, "DataExpress"], [11, "DataView"]] The "sort" command sorts by the first item in each array, as can be seen by switching the values:>> products.map { |p| [p.name, p.id] }.sort=> [["DataCollector", 7], ["DataExpress", 10], ["DataMonitor", 9], ["DataTranslator", 8], ["DataView", 11]] So, how do I get the sort order I want from the second example, but keep the structure from the first example so the select list will display correctly? Thanks. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 9, 2009, at 3:47 PM, partydrone wrote:> > I want to pull data from the database once, but do multiple sorts on > different columns. > > I''m pulling a list of products from the database. In my view, I want > to create two select lists: one sorted by the product name; one sorted > by part number. I know I can map the values into a new array or use > "options_from_collection_for_select," but I''m having a little trouble > with sorting. > >>> products.map { |p| [p.id, p.name] }.sort > => [[7, "DataCollector"], [8, "DataTranslator"], [9, "DataMonitor"], > [10, "DataExpress"], [11, "DataView"]] > > The "sort" command sorts by the first item in each array, as can be > seen by switching the values: > >>> products.map { |p| [p.name, p.id] }.sort > => [["DataCollector", 7], ["DataExpress", 10], ["DataMonitor", 9], > ["DataTranslator", 8], ["DataView", 11]] > > So, how do I get the sort order I want from the second example, but > keep the structure from the first example so the select list will > display correctly?products.sort_by{|p| p.name}.map{|p| [p.name, p.id} is one way. -philip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 9, 10:47 pm, partydrone <partydr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to pull data from the database once, but do multiple sorts on > different columns. > > I''m pulling a list of products from the database. In my view, I want > to create two select lists: one sorted by the product name; one sorted > by part number. I know I can map the values into a new array or use > "options_from_collection_for_select," but I''m having a little trouble > with sorting. > > >> products.map { |p| [p.id, p.name] }.sortsort takes a block - you can make it compare anyway you want with that block, eg [1,2,3].sort {|x,y| x <=> y} is the same as [1,2,3] but [1,2,3].sort {|x,y| y <=> x } would sort in opposite order and [1,2,3,4,5,6,7,8].sort {|x,y| (x%5) <=> (y%5)} sorts according to residue mod 5. Fred> > => [[7, "DataCollector"], [8, "DataTranslator"], [9, "DataMonitor"], > [10, "DataExpress"], [11, "DataView"]] > > The "sort" command sorts by the first item in each array, as can be > seen by switching the values: > > >> products.map { |p| [p.name, p.id] }.sort > > => [["DataCollector", 7], ["DataExpress", 10], ["DataMonitor", 9], > ["DataTranslator", 8], ["DataView", 11]] > > So, how do I get the sort order I want from the second example, but > keep the structure from the first example so the select list will > display correctly? > > Thanks.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
This works, too: <%= select_tag(''product[name]'', options_for_select(@products.map { |p| [p.name, p.id] }.sort)) %> Thanks, guys. --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---