:-( what's wrong here


#1

fruits = ["orange", "apple", "banana", "pear", "grapes"]
frutis.sort! do { | firstfruit, secondfruit | firstfruit <=> secondfruit }
if firstfruit < secondfruit
-1
elsif firstfruit > secondfruit
1
else firstfruit = secondfruit
0
end
end


#2

First, if you use the sort method and the <=> operator, you don't need the if/else statement.

Second, if this code gives you an error, it's probably because you use "frutis.sort" instead of "fruits.sort".

Edit: Third, if you're passing a {} to sort!, you have to take out the do...end it's either one or the other.


#3

Why make it so complicated?

fruits.sort! {|first, second| second <=> first}

is all that's needed.


#4

Hi! I was struggling with this lesson as well. I used your code and it worked with one acception. The list is supposed to be displayed in descending order, so I put a .reverse! after sort. What I am struggling to comprehend is how the {|first, second| second <=> first} works as there are 5 objects in the array.

Am I overthinking this?


#5

The <=> operator is used to specify the order of the sort, nothing else. The order is then applied to all elements of the array.


#6

The <=> operator is used to specify the order of the sort, nothing else. The order is then applied to all elements of the array.

I think I am hung up on the syntax. The example says item1 <=> item2. To me that makes since if there are only 2 objects to sort. However, there are 5 objects to sort. My brain thinks it should read item1 <=> item5. Or item1 <=> item2 <=> item3 <=> item4 <=> item5


#7

First it will be applied to the first two elements (item1 <=> item2) and switch their positions if needed, then it will do the next comparisons (item2 <=> item3, etc...) until the array is over.

It's the sort method which does all this, we're only passing it the criteria we want.


#8

it will be applied to the first two elements (item1 <=> item2) and switch their positions if needed, then it will do the next comparisons (item2 <=> item3, etc...)

I think it just clicked. I, for whatever reason, assumed it was sorting everything at once instead of stepping through the array.

You're amazing. You'll be in the special thanks section of my first application.


#9

The sort function

second <=> first puts them in descending order, since it says to put bigger items before smaller items: If, say, the sort algorithm encounters 3, 7 in the array, 3 will be first, 7 will be second, and second <=> first will be 1 (because 7 > 3), which will make sort think that the 3 should come after the 7.

The sort function takes a comparison function as an argument. It uses the comparison function to determine the order a pair of values. It applies this function to several pairs of values in order to sort the array. (There are numerous different sort algorithms that compare different pairs in different orders, but the basic operation is to compare values pairwise).


#10

Thanks. I realized my problem is that I assumed the <=> sorted everything at once rather than stepping through the objects from left to right.


#11

<=> doesn't sort anything or step through anything ... the sort method does that (and it doesn't necessarily go left to right; it's not possible to sort an arbitrary array with a single left-to-right pass). <=> is just an operator that returns one of three values depending on the relative values of its arguments. It's a convenient operator to use in a comparison function, such as the one given to sort as an argument via a block.

Your problem is that you don't understand any of these concepts. I suggest restarting the lesson from the beginning and this time pay close attention. This advice comes from an expert programmer who has been doing this for half a century.


#12

@cssmaster41803 is correct, I was over-simplifying when I talked about the behaviour of sort. In fact, unless we have a look at the code of the sort method, we have no way of knowing how exactly it will sort the array (there are many algorithms for sorting), but we know that it will use successive comparisons between two elements of the array, and that's the criteria of this comparison that we're giving to sort.

The behaviour I was (incompletely) describing is called the bubble sort and is the most simple sort there is (you basically push the greatest element to the end of the array and then the second greatest to the end - 1, etc), but it's most likely not what sort uses because it's not optimal.