我想通過對象last_name對對象數組排序,但總是將某個對象放在數組的第一個位置。 爲了實現這一點,我已經嘗試了很多,但總是失敗的數組結果。這裏是我的嘗試之一,因爲我不能運行的結果數組對象(例如@team.each do |m| puts m.username end
失敗)的任何方法,其中失敗:Ruby:排除一個異常的數組
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
感謝您的幫助。
我必須同意,你的解決方案更好,更快。謝謝! :) – alexschomb