< :the previous in number
^ :the list in numerical order
> :the next in number
P :the previous artilce (have the same parent)
N :the next (in thread)
|<:the top of this thread
>|:the next thread
^ :the parent (reply-to)
_:the child (an article replying to this)
>:the elder article having the same parent
<:the youger article having the same parent
---:split window and show thread lists
| :split window (vertically) and show thread lists
~ :close the thread frame
.:the index
..:the index of indices
Marcin Mielyski skrev:
> Einar Hst 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}
>
> lopex
That solution requires some_field to be 'naturally' ordered, though,
doesn't it? (I'm very new to Ruby...) What if some_field contains a
string, and I want 'Oranges' to be sorted before 'Apples'? Actually, I'm
writing a card game, so I want 'Spades' < 'Hearts' < 'Clubs' < 'Diamonds'.
- Einar