From: mortee Date: 2007-10-18T01:07:39+09:00 Subject: Re: sort_by Paulo Carvalho wrote: > 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