-1

I have a problem with my sorting function. Namely, my code works only in Firefox, but few months ago it was working on every browser. Do you have any solution for that? Every sorting function is similar, so I'm pasting only one of them:

Here's my fiddle: https://jsfiddle.net/zfa6qkt5/13/

    handleSortNoteAsc = () => {
    let sortedAsc = this.state.data.sort(function(a, b) {return a.note > b.note});
    this.setState({
        data: sortedAsc
    })
};

handleSortNoteDesc = () => {
    let sortedDesc = this.state.data.sort(function(a, b) {return a.note < b.note});
    this.setState({
        data: sortedDesc
    })
};
adiga
  • 34,372
  • 9
  • 61
  • 83
Dorian
  • 3
  • 4
  • 2
    Please add your code to the question instead of linking to an external website. – TidyDev Jan 14 '19 at 09:03
  • I added example of one of the sorting functions. – Dorian Jan 14 '19 at 09:07
  • It's similar, but it doesn't tell me why it was working 6 months ago – Dorian Jan 14 '19 at 09:13
  • It may not have worked 6 months ago either, the size of your array will change the algorithm used by the browser and you may very well have different results on the same browser depending on the input. – Kaiido Jan 14 '19 at 09:20
  • Maybe you added some more code and this new code has some error which chrome detecs and does not run while it runs in firefox because it handles errors a bit differently. So maybe check your latest code since 6 months ago and see if this has any errors. Open chrome console and check for errors – Nikos M. Jan 14 '19 at 09:24
  • Console showed no errors. Can it be related to chrome version? – Dorian Jan 14 '19 at 09:42

1 Answers1

0

Webkit requires the comparison function to return an integer rather than a boolean, otherwise it does nothing with no error.

Array.sort browser differences

Sort you array like this:

[3,1,4,2,5].sort(function(a,b) {
    return a - b;
});

Not like this:

[3,1,4,2,5].sort(function(a,b) {
    return a > b;
});
Daniel Doblado
  • 2,481
  • 1
  • 16
  • 24
  • ...This quote... No, they do accept floats **and booleans**, as long as you use `false` to mean `0`, and `true` to mean `1` and anything that would evaluate to `<0` for `-1` you are fine. – Kaiido Jan 14 '19 at 09:39