Hi all, I want to sort the objects of array based on two attributes.I want sort an employee class based on his salary and name , so that if two person has same salary it should be sorted with name. Lets say for example the employee objects has following name and salary. name salary d 100 c 200 b 50 a 100 so in this case the result which i expect is name salary c 50 a 100 d 100 b 200 Note:In the above example for salary 100 the sorting is done alphabetically, but initially(before sorting) ''d'' came first and then ''a'' came.so basically i want to sort the array with more than one order. Can any one help me to solve it ?? -- 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 -~----------~----~----~----~------~----~------~--~---
Michael Wang
2007-Mar-26 13:24 UTC
Re: sorting an array based on two attributes of objects
senthil wrote:> Hi all, > I want to sort the objects of array based on two attributes.I want > sort an employee class based on his salary and name , so that if two > person has same salary it should be sorted with name. > Lets say for example the employee objects has following name and > salary. > > name salary > d 100 > c 200 > b 50 > a 100 > > > so in this case the result which i expect is > > name salary > c 50 > a 100 > d 100 > b 200 > > Note:In the above example for salary 100 the sorting is done > alphabetically, but initially(before sorting) ''d'' came first and then > ''a'' came.so basically i want to sort the array with more than one order. > Can any one help me to solve it ?? >Use sort_by and have it return an array of the two objects you want sorted: [26]irb irb(main):001:0> myarray = [{:name => "d", :salary => 100}, {:name => "c", :salary => 200}, {:name => "a", :salary => 100}] => [{:salary=>100, :name=>"d"}, {:salary=>200, :name=>"c"}, {:salary=>100, :name=>"a"}] irb(main):002:0> myarray.sort_by {|h| [h[:salary], h[:name]] } => [{:salary=>100, :name=>"a"}, {:salary=>100, :name=>"d"}, {:salary=>200, :name=>"c"}] -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Mar-26 13:24 UTC
Re: sorting an array based on two attributes of objects
On Mar 26, 2007, at 8:50 AM, senthil wrote:> Hi all, > I want to sort the objects of array based on two attributes.I > want > sort an employee class based on his salary and name , so that if two > person has same salary it should be sorted with name. > Lets say for example the employee objects has following name and > salary. > > name salary > d 100 > c 200 > b 50 > a 100 > > > so in this case the result which i expect is > > name salary > c 50 > a 100 > d 100 > b 200 > > Note:In the above example for salary 100 the sorting is done > alphabetically, but initially(before sorting) ''d'' came first and then > ''a'' came.so basically i want to sort the array with more than one > order. > Can any one help me to solve it ??[ [''d'',100], [''c'',200], [''b'',50], [''a'',100] ].sort_by {|ns| [ ns.last, ns.first ]} => [["b", 50], ["a", 100], ["d", 100], ["c", 200]] An array compares on its elements. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for some of your replies for the lst post i need small modification in that post .. i want to sort the salary in descending order. i have explained the same quesion again.... Hi all, I want to sort the objects of array based on two attributes.I want sort an employee class based on his salary in descecding order and name , so that if two person has same salary it should be sorted with name. Lets say for example the employee objects has following name and salary. name salary d 100 c 200 b 50 a 100 so in this case the result which i expect is name salary b 200 a 100 d 100 c 50 Note:In the above example for salary 100 the sorting is done alphabetically, but initially(before sorting) ''d'' came first and then ''a'' came.so basically i want to sort the array with more than one order. Can any one help me to solve it ?? -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Mar-26 15:00 UTC
Re: sorting an array based on two attributes of objects
On Mar 26, 2007, at 9:57 AM, senthil wrote:> Thanks for some of your replies for the lst post i need small > modification in that post .. i want to sort the salary in descending > order. i have explained the same quesion again.... > > Hi all, > I want to sort the objects of array based on two attributes.I > want > sort an employee class based on his salary in descecding order and > name > , so that if two > person has same salary it should be sorted with name. > Lets say for example the employee objects has following name and > salary. > > name salary > d 100 > c 200 > b 50 > a 100 > > > so in this case the result which i expect is > > name salary > b 200 > a 100 > d 100 > c 50 > > Note:In the above example for salary 100 the sorting is done > alphabetically, but initially(before sorting) ''d'' came first and then > ''a'' came.so basically i want to sort the array with more than one > order. > Can any one help me to solve it ?? >[ [''d'',100], [''c'',200], [''b'',50], [''a'',100] ].sort_by {|ns| [ - ns.last, ns.first ]} => [["b", 50], ["a", 100], ["d", 100], ["c", 200]] An array compares on its elements. (The only change from my earlier response is to negate the salary for sorting purposes.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---