title: How does sort function work in JavaScript, along with compare function
body: <p>As already asked: how does sort function work in JavaScript, along with the <code>compare</code> function?
  If I have an array, and I do <code>array.sort(compare)</code> now it was written in the book that if the <code>compare</code> function returns <code>a-b</code> (two indices of the array) then it works based on the fact that whether the result is greater than 0, less than 0 or equal to 0. But, how exactly does it work? I could not work it out.</p>