Thanks for reply :clap: Unfortunately, this is disappointing results :expressionless:
rampageX wrote:Chinese OS, the bottom 3x arrays all sort the two Chinese words correctly.
Strange. In array #3, there is the SAME sorting method as we used in previous Files app version, which Chinese users reported was correct. I think you also reported it was sorting correctly. The below Chinese sorting words is wrong?
["10name", "2name", "aa", "BBB", "xx", "zèbre", "écureuil", "中国", "非洲"]
This means that it's not able to sort Chinese if there are mixed character sets. I have tested tinyfilemanager.github.io and
datatables.js, and it sorts Chinese characters words the same as above (because it uses the same sorting function).
rampageX wrote:dont know how 'zèbre' 'écureuil' will be sort
This is solved correctly by
localeCompare() used in current Files app version. As you can see in last three arrays (both mine and yours), "écureuil" correctly comes after "BBB", before "xx".
rampageX wrote:2name 10name aa BBB xx 非洲 中国
..., but i am sure all the latin words will be front of chinese words.
I don't see how that is possible with any Javascript code. Anyone please feel free to try from my jsbin:
https://jsbin.com/vatoyuboka/edit?js,console
Conclusion
Unless anyone has any technical suggestions (or examples) for correctly sorting arrays of mixed Chinese and latin from Javascript, then I can only add
additional OPTIONS in next release.
1. Option
localeCompare
This will be kept as
default sorting option, because it sorts correctly in all cases except those reported by Chinese language users. Not only does it sort numbers naturally (2<10), but it also sorts unicode correctly (at least latin and/or in most OS/browsers). On questions about the
"correct way" to sort alphanumeric strings, localeCompare() is considered the correct answer for modern browsers.
https://stackoverflow.com/a/38641281/3040364
There are a few Javascript sorting libraries available [
1,
https://github.com/javve/natural-sort,
3,
4,
5], but they are no longer updated because they have been considered replaced by localeCompare() for modern browsers. This has been tested in depth:
https://stackoverflow.com/a/26295229/3040364
Perhaps one of the libs above might work for Chinese/mixed? :thinking: I don't know, but it's backwards to use non-native and slow custom sorting functions when there is already a native option in place.
2. Custom collator() function
I will expose the
Intl.Collator() function used in Files app for localeCompare(), so that anyone can edit parameters from config if they find a solution that works for them. In Files app, it looks like this (language undefined = automatic).
new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
User @amwpsaa tested that it worked in Chinese when setting the locale to 'de', although we don't know why and it's not logical.
new Intl.Collator('de', { numeric: true, sensitivity: 'base' });
3. New option basic sort
This is the sorting method used in previous Files app, and was reported working fine with Chinese by both @amwpsaa and @rampageX. Basically it just involves lowercase
a < b sorting, which resolves nicely in modern browsers and is fast. What this sort method does NOT do: 1. It does not understand that 2<10 (it just looks at first letter), and 2. It cannot sort mixed unicode (it will sort ["a", "z", "é"] instead of ["a", "é", "z"]. In many cases however, this will work nice and fast for many users who are not picky about these two flaws.
4. Custom sort function
I was considering allowing custom function for "alphanumeric" sort, so that users could write their own function. However, I don't see much point in this if nobody can write a Javascript sort function that resolves the issue with Chinese, noted in this post.
So for next release coming soon, expect #1-3. Unless anyone has and further suggestions :pray:
Happy Chinese New Year (yesterday)! :flag_cn:🧧🧨:dragon::tangerine: