From: "Marcin Mielżyński" Date: 2006-03-08T23:48:41+09:00 Subject: Re: Newbie: sorting an array of custom objects Marcin Mielżyński wrote: > Einar Høst wrote: >> Hi, >> >> As a project for learning Ruby, I'm writing a simple game. I have some >> simple objects that I would like to be able to sort 'automatically' in >> an array. In Java, I can implement the Comparable interface to make >> the Array.sort method do this for me. I'm sure I can do something >> similar or simpler i Ruby, I just don't know how. Can anyone help? An >> even more elegant solution would be to able to say to the array 'just >> put this object where it belongs', without really having to sort the >> whole array. >> >> Thanks a lot! >> >> Kind regards, >> Einar > > arr.sort_by{|obj| obj.some_field} > there is also another method for array sorting (actually it was the first in standard library) arr.sort{|a,b| a.some_field <=> b.some_field} if you define <=> operator for your class def <=> arg @some_field <=> arg.some_field end ther you will be able to arr.sort lopex