Sandeep Gudibanda
2008-Jun-01 19:11 UTC
Sorting an array of Hash on two fields one of it is optional
Hi,
I need to sort an array of Hash - an array whose elements are hash
variable - a hash of name and email. Name is an optional field , ie it
can be nil but email is always not NULL.
How do I sort the array of these hash variables? SOrt on names. But if
name is nil, then sort on emailID.
@contacts = @my_contacts.sort! do |a,b|
if a["name"] != nil && b["name"] != nil
n = a["name"] <=> b["name"]
end
if a["name"].nil?
n == 0 ? a["email"] <=> b["name"] :n
else
n == 0 ? a["name"] <=> b["email"]:n
end
n == 0 ? a["email"]<=> b["email"]:n
end
1. Name = A email=Z@a.com
2. Name = B email=b@a.com
3. Name = nil email=a@c.com
sort on this shud result in
1. Name = A email=Z@a.com
3. Name = nil email=a@c.com - Name A wins over name nil
2. Name = B email=b@a.com - email a@c.com wins over name B
But i am getting comparision of hash and hash failed - Error.
Pleas help.
Regards,
Sandeep G
--
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
-~----------~----~----~----~------~----~------~--~---
Brandon Keepers
2008-Jun-02 18:06 UTC
Re: Sorting an array of Hash on two fields one of it is optional
On Sun, Jun 1, 2008 at 3:11 PM, Sandeep Gudibanda < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > I need to sort an array of Hash - an array whose elements are hash > variable - a hash of name and email. Name is an optional field , ie it > can be nil but email is always not NULL. > > How do I sort the array of these hash variables? SOrt on names. But if > name is nil, then sort on emailID. > > @contacts = @my_contacts.sort! do |a,b| > > if a["name"] != nil && b["name"] != nil > n = a["name"] <=> b["name"] > end > if a["name"].nil? > n == 0 ? a["email"] <=> b["name"] :n > else > n == 0 ? a["name"] <=> b["email"]:n > end > n == 0 ? a["email"]<=> b["email"]:n > > end > > 1. Name = A email=Z@a.com > 2. Name = B email=b@a.com > 3. Name = nil email=a@c.com > > sort on this shud result in > 1. Name = A email=Z@a.com > 3. Name = nil email=a@c.com - Name A wins over name nil > 2. Name = B email=b@a.com - email a@c.com wins over name B > > But i am getting comparision of hash and hash failed - Error.I would just do: @contacts = @my_contacts.sort_by {|c| "#{c[:name]}#{c[:email]}" } This sorts by a new string that is the combination of the name and email. If the name is nil, nothing will be inserted and it will just have the email Brandon -------------------------------------------------------------------------------- Sessions by Collective Idea: Ruby on Rails training in a vacation setting http://sessions.collectiveidea.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 -~----------~----~----~----~------~----~------~--~---
Sandeep Gudibanda
2008-Jun-04 18:14 UTC
Re: Sorting an array of Hash on two fields one of it is opti
Thats a cool solution! Thanks Brandon! -- 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 -~----------~----~----~----~------~----~------~--~---