React-native: [JSC] function (Array.sort) won't sort Array

Created on 8 Nov 2015  路  5Comments  路  Source: facebook/react-native

var a = [1,3,2];
a.sort((num1, num2) => num1 > num2);
Global.alert('' + a);

And it logs [1,3,2],function sort does nothing

Locked

Most helpful comment

Yes, I have just tried

var a = [1,3,2];
a.sort((num1, num2) => num1 > num2 ? -1 : 1);
Global.alert('' + a);

It works

All 5 comments

Same result on iOS and Android, also checked to see if value was just returned and it is not. see: https://rnplay.org/apps/yHvHGg

Note that this executes as expected in Chrome:

var a = [1,3,2];
a.sort((num1, num2) => num1 > num2);
alert('' + a);

Note that if you leave out the compare function, this works on both iOS and Android: https://rnplay.org/apps/n92mLg

Oddly, the compare function does get called with the expected args -- I tested this by just alert'ing the each of the values within the compare function.

Doesn't the sorter need to return a number instead of a boolean?

Yes, I have just tried

var a = [1,3,2];
a.sort((num1, num2) => num1 > num2 ? -1 : 1);
Global.alert('' + a);

It works

Thanks! @ide @brentvatne ,I'll close this issue

Thanks @LeoJiaXin

Was this page helpful?
0 / 5 - 0 ratings