On 3/11/08, Harm Aa <nepaccount / gmail.com> wrote: > Peter Hug wrote: > > If class X has a method <=>(aX), I can sort an Array containing > > instances of X using Array.sort!. > > > > What I really would like is an array that is always ordered. IOW, I want > > the object to be inserted at the correct location inside the array when > > the object is added to the array. > > > > Is there an efficient way to do this? > > > > Pete > > > One option is to subclass Array and override the methods you use to > insert the elements. For example, consider the following: > > class OrderedArray < Array May I humbly suggest that SortedArray might be a more accurate choice of name. Arrays are by nature ordered, it's just that the order doesn't happen to be determined by the sort order of the elements. > Another option is to use delegation to wrap the Array, and just > implement the methods you desire: A much better choice, but. > class OrderedArray < Array Should be just class OrderedArray No need to subclass Array. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/