Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return +-1 or 0 in naturalCmp #324

Merged
merged 3 commits into from
Nov 28, 2014

Conversation

megawac
Copy link
Contributor

@megawac megawac commented Nov 18, 2014

Fixes #323

@stoeffel
Copy link
Collaborator

This looks good to me.
I think all of the usecases are [str1, str2].sort(_.naturalCmp); or if (_.naturalCmp(str1, str2) >= 1), since I guess everyone who ever used naturalCmp thought it works this way anyway. So I think a minor release is okay.
But I think a description in the readme would be nice.

@megawac
Copy link
Contributor Author

megawac commented Nov 21, 2014

Most likely true, I only noticed this issue because it was breaking several of my unit tests for an alternate compare method which delegates to str.naturalCmp

@stoeffel
Copy link
Collaborator

I will test it more tomorrow and merge it then.

@stoeffel
Copy link
Collaborator

Hmm... it looks like it's not uncommon to return something else than 1, 0 and -1 from
a comparator.
Checkout https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

function compareNumbers(a, b) {
  return a - b;
}

or http://stackoverflow.com/questions/17382091/javascript-sort-comparator-function

I don't think the current implementation is a bug and after rethinking it I would rather not merge this PR and risk breaking someone else's code.

I would recommend you to use Math.sign or a polyfill in your code if it is crucial to you to return 1,0,-1.

return Math.sign(str.naturalCmp(this.prerelease.join("."), other.prerelease.join(".")));

@megawac
Copy link
Contributor Author

megawac commented Nov 22, 2014

I'd say if not a bug its at least inconsistent. I'm on my phone so I'm not
going to provide examples but I have a hunch that someone doing

If (compare(a,b) == 1)

Would be more likely than someone doing

if (Compare(a,b) > 556)

Especially as I haven't seen this behavior reported before. I'm under the
impression that this may fix more broken edge than break conditions.

Also the other commits correct some slightly off logic
On Nov 22, 2014 12:04 PM, "Christoph Hermann" [email protected]
wrote:

Hmm... it looks like it's not uncommon to return something else than 1, 0
and -1 from
a comparator.
Checkout
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

function compareNumbers(a, b) {
return a - b;
}

or
http://stackoverflow.com/questions/17382091/javascript-sort-comparator-function

I don't think the current implementation is a bug and after rethinking it
I would rather not merge this PR and risk breaking someone else's code.

I would recommend you to use Math.sign or a polyfill in your code if it is
crucial to you to return 1,0,-1.

return Math.sign(str.naturalCmp(this.prerelease.join("."), other.prerelease.join(".")));


Reply to this email directly or view it on GitHub
#324 (comment)
.

@stoeffel
Copy link
Collaborator

Yes you may have a point with that. And I like the "of topic" changes and the tests.
So if we agree that it isn't a bug, it goes into the next major release (2.5.0) together with issue #326, if we deside to change that too.
It would be great if you could add something to the README, to make it absolutely clear to users.

Thanks for the PR.

stoeffel added a commit that referenced this pull request Nov 28, 2014
Always return +-1 or 0 in naturalCmp
@stoeffel stoeffel merged commit 2facec6 into esamattis:master Nov 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

naturalCmp doesn't always return 1, 0 or -1
2 participants