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[0] bigger than arr[1]? no everything stays
step 2: is arr[1] bigger than arr[2]? yes so we swich them
step 3:is arr[2] bigger than arr[3] no everything stays
step 4: is arr[3] bigger than arr[4]? 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
[10,15,0,20,30]
[10,0,15,20,30]
then finaly
[0,10,15,20,30]
so whats the a-b thing?
well if a is bigger than b its a positive number and should not be moved.(like arr[0]-arr[1]
if a is smaller than b then it will return a negative number and should be switched(like arr[4] -arr[3]
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.