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

Array sort with comparator works with remote debugging on but doesn't work with it off #21428

Closed
3 tasks
FrickenHamster opened this issue Sep 30, 2018 · 3 comments
Closed
3 tasks
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@FrickenHamster
Copy link

Environment

React Native Environment Info:
System:
OS: Windows 7
CPU: x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
Memory: 18.15 GB / 31.89 GB
Binaries:
Yarn: yarn install v0.21.3
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 58.85s. - ~\AppData\Roaming\npm\yarn.CMD
npm: 5.4.2 - C:\Program Files\nodejs\npm.CMD
IDEs:
Android Studio: Version 3.2.0.0 AI-181.5540.7.32.5014246

I am running RN 0.57

Description

It seems if you are running on an android device, I've tested on the emulator and my physical devices and you call sort on an array with a comparator function ie:
const a = [4,6,2,3]; a.sort((a, b) => a > b); Alert.alert(a.join(','))

If you are remote debugging, it'll run correctly and output 2,3,4,6

However, if you are not remote debugging, it'll output 4,6,2,3

Reproducible Demo

In any react native project, make a button and in the callback have it run something like
const a = [4,6,2,3]; a.sort((a, b) => a > b); Alert.alert(a.join(','))

@FrickenHamster
Copy link
Author

After some help from a guy in discord and thinking, I've discovered the problem is because debug mode uses chrome's js engine, which accepts truthy values from the comparator function, while the default react native engine only accepts numbers.
Thus I was able to fix it by appending a ? 1 : -1 after each truthy statement.
Imo this behavior should be fixed to be consistent between debug and non debug mode, but I don't know if it is possible.

@zibs
Copy link

zibs commented Sep 30, 2018

Yep this is a semi well known issue that definitely catches most people at least once - there is some talk about upgrading the Android JSC here and elsewhere: https://github.com/react-community/jsc-android-buildscripts

@kelset
Copy link
Contributor

kelset commented Dec 17, 2018

Yes, this issue has been reported already - please refer to #19737

@kelset kelset closed this as completed Dec 17, 2018
@facebook facebook locked as resolved and limited conversation to collaborators Dec 17, 2019
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Dec 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants