From: Daniel Harple Date: 2006-03-08T23:55:55+09:00 Subject: Re: Newbie: sorting an array of custom objects On Mar 8, 2006, at 3:38 PM, Einar H�st wrote: > 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. You could use a SortedSet, however this has a downside. A Set contains no duplicate objects. require 'set' set = SortedSet.new set << 3 set << 1 set << 10 set << 3 p set # -> # -- Daniel