sort_by

P

Paulo Carvalho

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.
 
F

Farrel Lifson

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}
 
P

Paulo Carvalho

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
 
M

mortee

Paulo said:
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
 
R

Rob Biedenharn

Farrel said:
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

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://agileconsultingllc.com
(e-mail address removed)
 
M

mortee

Paulo said:
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?

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
 
J

John Joyce

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"]
 
J

John Joyce

Farrel said:
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-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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
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.
 
P

Paulo Carvalho

Thank you all.

Your hints were very helpfull to resolve my problem.

Best regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,269
Messages
2,571,349
Members
48,028
Latest member
Rigor4

Latest Threads

Top