[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamic sort_by criteria, making it DRY...

Josselin

6/28/2007 11:41:00 AM

I am presently using a sort_by on my array

=> buddies = user_buddies.sort_by {|item| item.last_seen_at}

I would like to be able to do the same , based on a variable
@criteria set to the item key to be used

@criteria = 'last_seen_at' @criteria = 'display_name' ......

=> buddies = user_buddies.sort_by {|item| item.<@criteria> }

what could be the best way to do it ?

Depending upon another variable @reverse_order = true or false, I would
like to perform or not teh reverse! action

=> buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse!

I believe it should be

=> buddies = @reverse_order ? user_buddies.sort_by {|item|
item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria>
}.reverse!

thanks for your enlightment !

joss

4 Answers

Robert Klemme

6/28/2007 11:59:00 AM

0

On 28.06.2007 13:41, Josselin wrote:
> I am presently using a sort_by on my array
>
> => buddies = user_buddies.sort_by {|item| item.last_seen_at}
>
> I would like to be able to do the same , based on a variable
> @criteria set to the item key to be used
>
> @criteria = 'last_seen_at' @criteria = 'display_name' ......
>
> => buddies = user_buddies.sort_by {|item| item.<@criteria> }
>
> what could be the best way to do it ?
>
> Depending upon another variable @reverse_order = true or false, I would
> like to perform or not teh reverse! action
>
> => buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse!
>
> I believe it should be
>
> => buddies = @reverse_order ? user_buddies.sort_by {|item|
> item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria>
> }.reverse!
>
> thanks for your enlightment !

buddies = user_buddies.sort_by {|it| it.send @criteria}
buddies.reverse! if @reverse_order

Kind regards

robert

Josselin

6/28/2007 9:19:00 PM

0

On 2007-06-28 13:58:47 +0200, Robert Klemme <shortcutter@googlemail.com> said:

> On 28.06.2007 13:41, Josselin wrote:
>> I am presently using a sort_by on my array
>>
>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}
>>
>> I would like to be able to do the same , based on a variable
>> @criteria set to the item key to be used
>>
>> @criteria = 'last_seen_at' @criteria = 'display_name' ......
>>
>> => buddies = user_buddies.sort_by {|item| item.<@criteria> }
>>
>> what could be the best way to do it ?
>>
>> Depending upon another variable @reverse_order = true or false, I would
>> like to perform or not teh reverse! action
>>
>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse!
>>
>> I believe it should be
>>
>> => buddies = @reverse_order ? user_buddies.sort_by {|item|
>> item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria>
>> }.reverse!
>>
>> thanks for your enlightment !
>
> buddies = user_buddies.sort_by {|it| it.send @criteria}
> buddies.reverse! if @reverse_order
>
> Kind regards
>
> robert

Thanks Robert cannot be dryest.... I am so ashame of my solution that
I don't want to publish my code...

Josselin

6/29/2007 6:12:00 AM

0

On 2007-06-28 23:18:36 +0200, Josselin <josselin@wanadoo.fr> said:

> On 2007-06-28 13:58:47 +0200, Robert Klemme <shortcutter@googlemail.com> said:
>
>> On 28.06.2007 13:41, Josselin wrote:
>>> I am presently using a sort_by on my array
>>>
>>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}
>>>
>>> I would like to be able to do the same , based on a variable
>>> @criteria set to the item key to be used
>>>
>>> @criteria = 'last_seen_at' @criteria = 'display_name' ......
>>>
>>> => buddies = user_buddies.sort_by {|item| item.<@criteria> }
>>>
>>> what could be the best way to do it ?
>>>
>>> Depending upon another variable @reverse_order = true or false, I would
>>> like to perform or not teh reverse! action
>>>
>>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse!
>>>
>>> I believe it should be
>>>
>>> => buddies = @reverse_order ? user_buddies.sort_by {|item|
>>> item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria>
>>> }.reverse!
>>>
>>> thanks for your enlightment !
>>
>> buddies = user_buddies.sort_by {|it| it.send @criteria}
>> buddies.reverse! if @reverse_order
>>
>> Kind regards
>>
>> robert
>
> Thanks Robert cannot be dryest.... I am so ashame of my solution that
> I don't want to publish my code...

btw , where should I write the 'downcase' method for strings

I tried to write it into the @criteria (@criteria = 'display_name.downcase' )
which seems to be the right place..

but I got an error
undefined method `display_name.downcase' for #<User:0x34bc34c>

thanks for your complementary solution... ;-))

joss

Robert Klemme

6/29/2007 7:52:00 AM

0

On 29.06.2007 08:12, Josselin wrote:
> On 2007-06-28 23:18:36 +0200, Josselin <josselin@wanadoo.fr> said:
>
>> On 2007-06-28 13:58:47 +0200, Robert Klemme
>> <shortcutter@googlemail.com> said:
>>
>>> On 28.06.2007 13:41, Josselin wrote:
>>>> I am presently using a sort_by on my array
>>>>
>>>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}
>>>>
>>>> I would like to be able to do the same , based on a variable
>>>> @criteria set to the item key to be used
>>>>
>>>> @criteria = 'last_seen_at' @criteria = 'display_name' ......
>>>>
>>>> => buddies = user_buddies.sort_by {|item| item.<@criteria> }
>>>>
>>>> what could be the best way to do it ?
>>>>
>>>> Depending upon another variable @reverse_order = true or false, I
>>>> would like to perform or not teh reverse! action
>>>>
>>>> => buddies = user_buddies.sort_by {|item| item.last_seen_at}.reverse!
>>>>
>>>> I believe it should be
>>>>
>>>> => buddies = @reverse_order ? user_buddies.sort_by {|item|
>>>> item.<@criteria> } : user_buddies.sort_by {|item| item.<@criteria>
>>>> }.reverse!
>>>>
>>>> thanks for your enlightment !
>>>
>>> buddies = user_buddies.sort_by {|it| it.send @criteria}
>>> buddies.reverse! if @reverse_order
>>>
>>> Kind regards
>>>
>>> robert
>>
>> Thanks Robert cannot be dryest.... I am so ashame of my solution that
>> I don't want to publish my code...
>
> btw , where should I write the 'downcase' method for strings
>
> I tried to write it into the @criteria (@criteria =
> 'display_name.downcase' )
> which seems to be the right place..
>
> but I got an error
> undefined method `display_name.downcase' for #<User:0x34bc34c>
>
> thanks for your complementary solution... ;-))
>
> joss

buddies = user_buddies.sort_by {|it| it.send(@criteria).downcase}

But this works only if you know that all criteria are strings.

Kind regards

robert