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 ...
1 answer
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
|



compareNumericnever returns a number less than zero, and this is blatantly incorrect behavior - andreymal