Sorting an ActiveRecord hash

Craig Jolicoeur wrote:

I've got a query like the following:

@tags = Tag.find(:all, :limit => 20, :conditions => "id != 22 and id !=
47" , :order => "count DESC")

My understanding is the ActiveRecord returns an array of hash objects to
@tags. so @tags is actually an array full of hashes.

the tags table has the following fields:

id, name, count, updated_at

The query returns the top 20 tags sorted by count into @tags

How can I then sort @tags by the 'name' field so I will have the top 20
tags sorted by alpha name?

@tags.sort! { |t| t.name }

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.

Jamey Cribbs wrote:

Craig Jolicoeur wrote:
  

I've got a query like the following:

@tags = Tag.find(:all, :limit => 20, :conditions => "id != 22 and id !=
47" , :order => "count DESC")

My understanding is the ActiveRecord returns an array of hash objects to
@tags. so @tags is actually an array full of hashes.

the tags table has the following fields:

id, name, count, updated_at

The query returns the top 20 tags sorted by count into @tags

How can I then sort @tags by the 'name' field so I will have the top 20
tags sorted by alpha name?

@tags.sort! { |t| t.name }
  
Oops. That should be:

  @tags.sort! { |a,b| a.name <=> b.name }

or, alternatively:

  sorted_tags = @tags.sort_by { |t| t.name }

Jamey

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.