From: Ammar Ali Date: 2010-11-13T21:40:12+09:00 Subject: Re: help sorting objects by their instance field Hmmm. for some reason my reply never made it. On Sat, Nov 13, 2010 at 2:38 PM, Ammar Ali wrote: > On Sat, Nov 13, 2010 at 2:17 PM, Aaron Haas wrote: >> I re-did the print method as you suggested but it is not sorting by the >> persons last name. It is just printing in the order in which they were >> added to array. >> Here is my print method >> >> # sort and print addresses in alphabetical order >>    def print_addresses >>      @persons.sort_by {|person| person.lname}  # sorts list by lname >>      @persons.each { |p| puts p }  # prints address book >> >>    end > > Remember, I gave incorrect information, so the sort is not "saved" in > the array, so you need to make a copy: > > def print_addresses >  sorted_persons = @persons.sort_by {|person| person.lname} >  sorted_persons.each { |p| puts p } > end > > Or, you can do it all on one line: > > @persons.sort_by {|person| person.lname}.each {|p| puts p} > > Regards, > Ammar