Ruby: Sort an array with an exception -


i want sort array of objects objects last_name, put object in first position of array. achieve tried lot failed resulting structure of array. here 1 of tries fails because can't run methods on resulting array objects (e.g. @team.each |m| puts m.username end fails):

if @team = usergroup.find_by_name("team").users.sort_by(&:last_name)   if first_member = @team.detect{|m| m.username == "test"}     @team.unshift @team.reject{|m| m.username == "test"}   end end 

thanks help.

you can use sql's order capabilities, faster ruby (btw, should use find_by_attribute!). untested, play this:

@team = usergroup.find_by_name!("team").users.   order("username = 'test' desc, last_name asc") 

of course can use ruby same idea:

@team = usergroup.find_by_name!("team").users.   sort_by { |u| [u.username == 'test' ? 0 : 1, u.last_name] } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -