[lnkForumImage]
TotalShareware - Download Free Software

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


 

Paulo Carvalho

10/17/2007 2:51:00 PM

Hello

I have a list of rows which I want to order by a column.
I make it with
@values_ordered = @values.sort_by(&:id_column)

What if I want to inverse the order (like order by x desc in SQL)

How could I do that using sort_by? is that possible?

thanks

regards.
--
Posted via http://www.ruby-....

8 Answers

Farrel Lifson

10/17/2007 2:58:00 PM

0

On 17/10/2007, Paulo Carvalho <pjcarvalho@gmail.com> wrote:
> I have a list of rows which I want to order by a column.
> I make it with
> @values_ordered = @values.sort_by(&:id_column)
>
> What if I want to inverse the order (like order by x desc in SQL)
>
> How could I do that using sort_by? is that possible?

values_orderd = @values.sort_by{|value| -value.id_column}

Paulo Carvalho

10/17/2007 3:12:00 PM

0

Hello

Thanks for your quick answer.

However i am having a problem.

I used the syntax that you give to me like this :

To order (ASC way):
values_ordered = @demandes.sort_by{|value| value.eluxid}
(This one works fine)

To order (DESC way)
values_ordered = @demandes.sort_by{|value| -value.eluxid}
This one send to me a syntax error (I think he dont like the '-'):
undefined method `-@' for ["3CC11A"]:Array

(note: "3CC11A" is the column value of my first row)

Any idea of where is the problem?

Thanks again




Farrel Lifson wrote:
> On 17/10/2007, Paulo Carvalho <pjcarvalho@gmail.com> wrote:
>> I have a list of rows which I want to order by a column.
>> I make it with
>> @values_ordered = @values.sort_by(&:id_column)
>>
>> What if I want to inverse the order (like order by x desc in SQL)
>>
>> How could I do that using sort_by? is that possible?
>
> values_orderd = @values.sort_by{|value| -value.id_column}

--
Posted via http://www.ruby-....

mortee

10/17/2007 3:56:00 PM

0

Rob Biedenharn

10/17/2007 4:05:00 PM

0

On Oct 17, 2007, at 11:12 AM, Paulo Carvalho wrote:
> Farrel Lifson wrote:
>> On 17/10/2007, Paulo Carvalho <pjcarvalho@gmail.com> wrote:
>>> I have a list of rows which I want to order by a column.
>>> I make it with
>>> @values_ordered = @values.sort_by(&:id_column)
>>>
>>> What if I want to inverse the order (like order by x desc in SQL)
>>>
>>> How could I do that using sort_by? is that possible?
>>
>> values_orderd = @values.sort_by{|value| -value.id_column}
>
> Hello
>
> Thanks for your quick answer.
>
> However i am having a problem.
>
> I used the syntax that you give to me like this :
>
> To order (ASC way):
> values_ordered = @demandes.sort_by{|value| value.eluxid}
> (This one works fine)
>
> To order (DESC way)
> values_ordered = @demandes.sort_by{|value| -value.eluxid}
> This one send to me a syntax error (I think he dont like the '-'):
> undefined method `-@' for ["3CC11A"]:Array
>
> (note: "3CC11A" is the column value of my first row)
>
> Any idea of where is the problem?
>
> Thanks again
>
> --
> Posted via http://www.ruby-....

If you don't have a way of giving your sortable value an inverse, you
may have to use .sort_by{|v|v.eluxid}.reverse or just use .sort {|
a,b| b.eluxid <=> a.eluxid } (note the swapping of the left- and
right-hand sides).

When @demandes.size is "small enough" it's not necessarily faster to
use sort_by than just sort (particularly if computing a DESC version
of the sort key is "expensive").

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com




mortee

10/17/2007 4:08:00 PM

0

John Joyce

10/17/2007 4:22:00 PM

0


On Oct 17, 2007, at 10:55 AM, mortee wrote:

> Paulo Carvalho wrote:
>> Hello
>>
>> I have a list of rows which I want to order by a column.
>> I make it with
>> @values_ordered = @values.sort_by(&:id_column)
>>
>> What if I want to inverse the order (like order by x desc in SQL)
>>
>> How could I do that using sort_by? is that possible?
>
> what about
>
> @values_ordered = @values.sort_by { |e| -e.id_column }
>
> mortee
>
>
Is it Rails? If so, ActiveRecord has a way to do this with a
database table's values by using a hash option, order => DESC
If you're using a database, you should consider ActiveRecord itself.
There is also Ruby DBI, which allows pretty much plain SQL action.
Either one will make life much easier working with a database.
There is also OG (another ORM)

The question I have is, Is your list of rows a multi-dimensional Array?
If so, and I am assuming it is, you can use the Array indices like
columns for sort_by
Use irb (and ri to check some docs) to compare :
Array.sort
Array.sort { |a, b| block }

sort can also take a block and might be what you want.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]

John Joyce

10/17/2007 4:26:00 PM

0


On Oct 17, 2007, at 11:04 AM, Rob Biedenharn wrote:

> On Oct 17, 2007, at 11:12 AM, Paulo Carvalho wrote:
>> Farrel Lifson wrote:
>>> On 17/10/2007, Paulo Carvalho <pjcarvalho@gmail.com> wrote:
>>>> I have a list of rows which I want to order by a column.
>>>> I make it with
>>>> @values_ordered = @values.sort_by(&:id_column)
>>>>
>>>> What if I want to inverse the order (like order by x desc in SQL)
>>>>
>>>> How could I do that using sort_by? is that possible?
>>>
>>> values_orderd = @values.sort_by{|value| -value.id_column}
>>
>> Hello
>>
>> Thanks for your quick answer.
>>
>> However i am having a problem.
>>
>> I used the syntax that you give to me like this :
>>
>> To order (ASC way):
>> values_ordered = @demandes.sort_by{|value| value.eluxid}
>> (This one works fine)
>>
>> To order (DESC way)
>> values_ordered = @demandes.sort_by{|value| -value.eluxid}
>> This one send to me a syntax error (I think he dont like the '-'):
>> undefined method `-@' for ["3CC11A"]:Array
>>
>> (note: "3CC11A" is the column value of my first row)
>>
>> Any idea of where is the problem?
>>
>> Thanks again
>>
>> -- Posted via http://www.ruby-....
>
> If you don't have a way of giving your sortable value an inverse,
> you may have to use .sort_by{|v|v.eluxid}.reverse or just use .sort
> {|a,b| b.eluxid <=> a.eluxid } (note the swapping of the left- and
> right-hand sides).
>
> When @demandes.size is "small enough" it's not necessarily faster
> to use sort_by than just sort (particularly if computing a DESC
> version of the sort key is "expensive").
>
> -Rob
>
> Rob Biedenharn http://agileconsult...
> Rob@AgileConsultingLLC.com
>
>
>
>
Good Point!
For large tables, it will be faster to use the database's abilities,
so if the data set is large and you want the speed, go for DBI or
ActiveRecord or OG or something.
Many databases have optimized abilities for this. DESC is one of those.

Paulo Carvalho

10/18/2007 8:34:00 AM

0

Thank you all.

Your hints were very helpfull to resolve my problem.

Best regards

John Joyce wrote:
> On Oct 17, 2007, at 11:04 AM, Rob Biedenharn wrote:
>
>>>>
>>> To order (ASC way):
>>> Any idea of where is the problem?
>> When @demandes.size is "small enough" it's not necessarily faster
>>
> Good Point!
> For large tables, it will be faster to use the database's abilities,
> so if the data set is large and you want the speed, go for DBI or
> ActiveRecord or OG or something.
> Many databases have optimized abilities for this. DESC is one of those.

--
Posted via http://www.ruby-....