From: Jeremy Bopp Date: 2010-11-13T02:13:29+09:00 Subject: Re: help sorting objects by their instance field On 11/12/2010 10:54 AM, Aaron Haas wrote: > Thank you guys so much for your helpful posts. I just started learning > ruby this week and it has been slow going. > > I read the api on array's sort methods and I did not understand them. Well, just keep in mind that the sort methods belong to Array, so you have to call them on an array rather than an element of an array. :-) > Does this line sort the array of persons? > > @persons.sort_by {|person| person.aLname} It should sort by the aLname property of each array element, but give it a try and see for yourself. You should get familiar with irb for simple things (like trying out array sorting), and don't fear running your code to see what it does. > do I need to save it to a new array variable? > > order_array = @persons.sort_by {|person| person.aLname} It depends on how you want to use it. If all you want to do is print the sorted list, you could just skip the assignment and immediately call #each on the array returned by #sort_by: @persons.sort_by {|person| person.aLname}.each |person| puts person.aLname end If you need to use the same sorted list again, it might be a good idea to store it somewhere before using it further, but in this case it doesn't appear necessary. -Jeremy