Hello everyone, I am trying to sort a randomly created array with coordinates. Sorting is easy - I compare the first coordinates. I wrote everything for repl.it for the test. Everything works fine there. As soon as I copy the EXACTLY SUCH code in the html document, it gives me two unsorted arrays. I'm just confused. Help with advice, otherwise I’m already starting a panic attack ...

On repl.it On repl.it

Pasted in html Pasted in html

Browser output Browser output

  • It's strange that it works for you somehow, because compareNumeric never returns a number less than zero, and this is blatantly incorrect behavior - andreymal

1 answer 1

I agree with andreymal, also now I do not understand how IT worked. But when I replaced return 0 with return -1, everything worked in the browser.

  • You need to return 0 in case of equality. Otherwise, equal elements will be re-sorted. Read about sorting stability. Generally speaking, the stability is influenced by the algorithm, but if the comparison of equal keys returns -1, any algorithm will be unstable. - A1essandro