Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Just a question on sorting.

a = ["r","l","l","o"]

a.sort! {|first,second,third,fourth| fourth <=> first }

Im trying to get grips with the custom sorting using the brackets and im trying to spell roll within the array can someone tell me why this isn't working?

1 Answer

Hi Alphonse,

The block for sort! should only take 2 parameters which are going to be 2 elements from the array.

Here's a link to the documentation for sort!: http://ruby-doc.org/core-2.2.0/Array.html#method-i-sort-21

The code samples show how to do a sort in ascending(default) order or descending order which is 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"]

The second example with the block should be what you're looking for.