# Sort\_by

**URL:** <https://www.ruby-forum.com/t/sort-by/117508>\
**Category:** Ruby\
**Created:** [October 17, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/sort-by/117508 "2007-10-17T16:51:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/sort-by/117508/1 "2007-10-17T16:51:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/sort-by/117508/2 "2007-10-17T16:58:47Z")

</div>

On 17/10/2007, Paulo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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}

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/sort-by/117508/3 "2007-10-17T17:12:08Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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}

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/sort-by/117508/4 "2007-10-17T18:05:32Z")

</div>

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/](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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 6:09pm UTC](https://www.ruby-forum.com/t/sort-by/117508/5 "2007-10-17T18:09:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/sort-by/117508/6 "2007-10-17T17:57:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/sort-by/117508/7 "2007-10-17T18:27:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 18, 2007, 10:34am UTC](https://www.ruby-forum.com/t/sort-by/117508/8 "2007-10-18T10:34:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crazybass](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb9ed0/32.png) [@crazybass](https://www.ruby-forum.com/u/crazybass)\
**Post date:** [October 17, 2007, 6:21pm UTC](https://www.ruby-forum.com/t/sort-by/117508/9 "2007-10-17T18:21:56Z")

</div>

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