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

Fix of the compare function #245

Merged
merged 1 commit into from
Feb 13, 2017
Merged

Conversation

iamstolis
Copy link
Contributor

The previous version of the compare function was not consistent (it did not follow the requirements on the compare function), see https://tc39.github.io/ecma262/#sec-array.prototype.sort for details. You can see that the compare function is not correct by using it to sort larger arrays. For example, if you sort the letters of a well known sentence:
"the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a>b).join('')
returns "uge d ac be e f otjhmpsnooqrothrkliuvwxyz" on Node.js 6.7.0. While "the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a > b ? 1 : (a < b ? -1 : 0)).join('') returns the correct result ' abcdeeefghhijklmnoooopqrrsttuuvwxyz'.

The previous version of the compare function was not consistent (it did not follow the requirements on the compare function), see https://tc39.github.io/ecma262/#sec-array.prototype.sort for details. You can see that the compare function is not correct by using it to sort larger arrays. For example, if you sort the letters of a well known sentence:
`"the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a>b).join('')`
returns `"uge d ac  be e  f otjhmpsnooqrothrkliuvwxyz"` on Node.js 6.7.0. While `"the quick brown fox jumps over the lazy dog".split('').sort((a,b) => a > b ? 1 : (a < b ? -1 : 0)).join('')` returns the correct result `'        abcdeeefghhijklmnoooopqrrsttuuvwxyz'`.
@jonschlinkert
Copy link
Member

Thanks I'm surprised I didn't catch that. I have other sorting libs that do this. thanks for the fix.

@jonschlinkert jonschlinkert merged commit 33d9172 into helpers:master Feb 13, 2017
@iamstolis
Copy link
Contributor Author

Thanks for your fast response!

I have other sorting libs that do this.

It is funny that you have mentioned that. I have a similar PR waiting for a merge jonschlinkert/array-sort#4 in that repository as well ;-).

@jonschlinkert
Copy link
Member

ha, well I might as well give up on sorting lol. Thanks, and sorry it has taken so long to get back to that issue. I'll try to get these merged and published today. I really appreciate it

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.

2 participants