19/19


#1

I can't seem to figure this one out..
here is my code:

fruits.sort! { |firstfruit, secondfruit|
firstfruit <=> secondfruit }
end


#2

Since we are using a block, we do not need to include end

fruits.sort! { |fruit1, fruit2| fruit2 <=> fruit1 }

#3

thank you, super helpful!


#4

is there any reason why we dont use end as we are using block


#5

also can you help me.....i cant understand this program


#6

Do you guys think there would be a problem using this way:

fruits = ["orange", "apple", "banana", "pear", "grapes"]

fruits.sort!.reverse! do |n|
n <=> n + 1
end

It works perfectly for me.


#7

Is more code than necessary:

fruits = ["orange", "apple", "banana", "pear", "grapes"]

fruits.sort!.reverse! 

fruits.each { |x| puts "#{x}" }

Output

pear
orange
grapes
banana
apple

#8

I like your code much better. Definitely is simplified and makes perfect sense. Big fan. It won't pass me with that way though. Yours is probably much better and more efficient but they are saying they want the <=> symbol and won't give the green button until it's in there unfortunately. I'll remember your way for future stuff for sure. Thanks a bunch!


#9

I wondered if this was a lesson requirement.

https://www.codecademy.com/en/courses/ruby-beginner-en-ET4bU/4/4

This will pass,

fruits.sort!.reverse! do |n|
#n <=> n + 1
end

and it works too. Why? Because reverse! did all the work. The do block has no effect. Consider the following...

fruits.sort! { |fruit1, fruit2| fruit2 <=> fruit1 }

Or we could stick to the do..end

fruits.sort! do | a, b | 
    b <=> a
end

#10

Oh very interesting! And yep, that link you have posted is the exercise. I think it's getting a little more clear now. Well I'm definitely going back to this exercise and I'm gonna experiment with all this. Thanks a lot! That's a big help!