Tyrel Denison
Tyrel Denison

Reputation: 476

Sorting An Array of Object by Different Attributes

I have an array of objects containing three different models. Two of them have a common attribute of category, and a third one that we just added doesn't. I'm wanting to sort the array alphabetically using the category name for the first two and the object's name for the third. Since they are all strings, this seems possible, but I don't know how to make it work.

My controller:

def index
@opportunities = Opportunity.non_historical_active.order("title")
@recommendations = Recommendation.active
@fast_content = FastContent.where(:approved => true)
@skills = (@recommendations + @opportunities + @fast_content)

Array displayed in my view:

<% Array(@skills.sort_by{ |skill| skill.opportunity_category.name}).each_with_index do |opp, i| %>

This array worked before we added the @fast_content variable to @skills.

Upvotes: 0

Views: 65

Answers (1)

Stefan
Stefan

Reputation: 114178

Assuming Opportunity and Recommendation should be sorted by opportunity_category.name and FastContent should be sorted by name, this would work:

@skills.sort_by { |skill|
  case skill
  when Opportunity, Recommendation
    skill.opportunity_category.name
  when FastContent
    skill.name
  end
}

Another option is to add a common method to all three classes:

class Opportunity < ActiveRecord::Base
  def sort_name
    opportunity_category.name
  end
end

class Recommendation < ActiveRecord::Base
  def sort_name
    opportunity_category.name
  end
end

class FastContent < ActiveRecord::Base
  def sort_name
    name
  end
end

And use that instead:

@skills.sort_by(&:sort_name)

Upvotes: 2

Related Questions