Sort_by

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.

On 17/10/2007, Paulo C. [email protected] 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

Farrel L. wrote:

On 17/10/2007, Paulo C. [email protected] 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}

On Oct 17, 2007, at 11:12 AM, Paulo C. wrote:

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

Thanks again


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

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 B. http://agileconsultingllc.com
[email protected]

Paulo C. wrote:

(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?

You can’t negate an Array, so that the order based on it be reversed.

You’ll have to either extract some numerical sort key out of your items
to sort, like value[0].to_i, which you can then negate (if that does the
trick); or otherwise you can use sort:

@demandes.sort { |value1, value2| value2.eluxid <=> value1.eluxid }

Note that in the comparison in the passed block, the order of the two
items is reversed, thus the resulting sort order would be reversed also.
This works on arbitrary comparable sort keys, as opposed to negating,
which can only be applied to numerics.

Finally, if for some reason you want to stick with sort_by, you can
#reverse the resulting sorted array afterwards.

mortee

Paulo C. 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

On Oct 17, 2007, at 11:04 AM, Rob B. 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.

Thank you all.

Your hints were very helpfull to resolve my problem.

Best regards

John J. wrote:

On Oct 17, 2007, at 11:04 AM, Rob B. 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.

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

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”]