From: MonkeeSage Date: 2007-12-03T18:25:00+09:00 Subject: Re: question about iterator On Dec 3, 1:57 am, Robert Klemme wrote: > 2007/12/2, MonkeeSage : > > > > > On Dec 2, 12:20 am, Paul Private wrote: > > > THanks Jordan > > > this was exactly what I was looking for > > > > Paul > > > Hi Paul, > > > Glad to help. One more thing: it would probably look nicer to hide > > that behind <=> > > > class Cursus > > ... > > def <=>(other) > > self.to_s <=> other.to_s > > end > > end > > > ...then you can call cursussen.sort w/o the block. > > Just a few picky remarks. :-) Sorting on the string representation, > while it may work, is unstable. It's better to sort based on members > because with your approach you create a dependency between to_s and > sorting which is neither necessary nor obvious. > > Also, I would only implement this in the class if it is the /default/ > order for instances of Cursus. > > Kind regards > > robert > > -- > use.inject do |as, often| as.you_can - without end I agree with you. In fact, I would have probably implemented the courses (Cursus) as ostructs or hashes in the backend. But given to current implementation, sorting on to_s amounts sorting on @naam, which is, I think, what the OP was asking for. So, while it is unstable, generally speaking; here, it amounts to a stable sort on a (default) instance variable. Regards, Jordan