I have an Array of active record objects that I need to sort by two attributes: similarity and activity, both are integers. I''m currently doing this by first sorting the array on the similarity field, and then using group_by to group them groups with the same ''similarity'' and then sorting those groups by activity. Flattening this whole thing our returns the original list sorted first by similarity and then by activity. Problem, this code is ugly, there has to be a cleaner way to do this. Here is the full method, its from a ActiveRecord model called ''Group''. http://pastie.caboo.se/70645 def related_groups #get the tags for this group tag_string = tags.collect(&:name) #find other groups with same tags groups_with_same_tags = Group.find_tagged_with(tag_string) if groups_with_same_tags.any? #delete self from list of groups groups_with_same_tags.delete(self) #delete duplicates groups_with_same_tags.uniq! #count how many tags they have in common (probably could cleanup with inject) groups_with_same_tags.each do |group| group.similarity = 0 tags.each do |tag| group.similarity = group.similarity + 1 if group.tags.include ?(tag) end end #sort based on similarity of tags groups_with_same_tags = groups_with_same_tags.sort{ |x,y| y.similarity<=> x.similarity } #group into a 2D array grouped by similarity, make sure to preserve priority of the similarity grouped_groups = groups_with_same_tags.group_by(&:similarity).sort{|x,y| y[0] <=> x[0]} #sort the 2nd dimension of the array(which is an array of the items of a specific similarity) based on activity sorted_groups = grouped_groups.collect{ |g| g[1].sort{|x,y| y.activity<=> x.activity }} #flatten it out into a 1D array and return sorted_groups.flatten! else [] end end 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 -~----------~----~----~----~------~----~------~--~---
Steve Rawlinson
2007-Jun-15 15:54 UTC
Re: Multiple Sorts on an array, help with bad code smell
I haven''t read the detail of your code (yet) but is there some reason you can''t form the array with groups = Group.find(:all, :order => "similarity, activity") steve -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
blinking bear
2007-Jun-15 16:11 UTC
Re: Multiple Sorts on an array, help with bad code smell
Steve - Unfortunately not, activity is not stored in the database. It is a dynamic tally of how many tags it has in common with the original object. thanks On 6/15/07, Steve Rawlinson <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > I haven''t read the detail of your code (yet) but is there some reason > you can''t form the array with > > groups = Group.find(:all, :order => "similarity, activity") > > steve > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try something like: groups_with_same_tags.sort! { |a, b| a.similarity == b.similarity ? b.activity <=> a.activity : b.similarity <=> a.similarity } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
blinking bear
2007-Jun-15 22:17 UTC
Re: Multiple Sorts on an array, help with bad code smell
perfect, tests pass, thanks! On 6/15/07, McPhage <McPhage-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Try something like: > > groups_with_same_tags.sort! { |a, b| a.similarity == b.similarity ? > b.activity <=> a.activity : b.similarity <=> a.similarity } > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
No problem--glad it worked. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---