ive seen that sort function before, and I spent hours trying to figure out what was going on. So I made an array, I used it, then on paper I drew out the same array and tried to match what was happening.
so we have this array [10,20,15,30,0]
sooooo…how to get this in order.
remember the computer cant just swt aside thought like we can it has to do it in an order.
so it looks at its steps
step 1:is arr bigger than arr? no everything stays
step 2: is arr bigger than arr? yes so we swich them
step 3:is arr bigger than arr no everything stays
step 4: is arr bigger than arr? yes so we switch them.
so now we have a slightly different array.
[10,15,20,0,30] closer right. well the sort function repeats itself until nothing is changed.
so I goes through the whole steps again.
so it will go again to get
so whats the a-b thing?
well if a is bigger than b its a positive number and should not be moved.(like arr-arr
if a is smaller than b then it will return a negative number and should be switched(like arr -arr
a-b makes it go low to high, it tells it to switch if a and b if b is bigger.
b-a makes it go high to low.
to really see how it goes, just put return -1; instead a-b;
watch how they shift. thw whole thing will switch until it reaches the end of the array.
sort is just looking for a thumbs up or a thumbs down, the a-b is either negative or positive.