-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Fixed JS sort comparator to be consistent between JS and PHP #7254
Conversation
Looks like it doesn't work correctly when sorting file names like "a.txt", "a10.txt", "a2.txt" |
|
This is needed to make the sort order exactly the same as in JS. |
|
|
I don't like how a single class is used for everything. |
@@ -17,7 +17,7 @@ | |||
"globals": { | |||
"console": true, | |||
"it": true, | |||
"itx": true, | |||
"xit": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a related change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, 'itx' was a mistake and one of the test uses "xit" (the phantomJS exclusion) which reminded me that this was wrong.
👎 Please extract the PHP comperator into its own class instead of using the Util class. Have a look at https://github.com/owncloud/core/pull/6778/files for inspiration. Is this maybe even the same or a related comperator? |
You are saying this is performing a "natural sort". There are also the functions natsort() and natcasesort(), so it would be useful to document the differences between your implementation and these in the class description. |
// Needed because PHP doesn't sort correctly when numbers are enclosed in | ||
// parenthesis, even with NUMERIC_COLLATION enabled. | ||
// For example it gave ["test (2).txt", "test.txt"] | ||
// instead of ["test.txt", "test (2).txt"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bantu documented here, that's one of the differences with natsort.
Also natsort didn't sort the umlauts properly.
@bantu I'll extract the comparator into a new class since it's already having three functions which is a bit too much for the Util class. This sort is not the same as the one you linked (already discussed with @blizzz) |
@PVince81 Sounds good. |
Rebased+squashed. I've moved the sorting algo into a separate class "OC_NaturalSort". Still need to test this branch on IE, Safari and other browsers. |
* | ||
*/ | ||
class OC_NaturalSort { | ||
private static $collator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything (methods too) should be non-static here. You should create instances of classes. In general, the number of "good use cases" for static is very limited.
@PVince81 More work is required here. From an archetictural standpoint it might be a good idea to have multiple Comparator implementation, and a single Sorter that takes an instance of a Comparator and does the sorting. Not sure whether that is easily possible, though. |
Opps, the file was supposed to be naturalsort.php, I might have committed it twice. I wasn't sure whether to use a comparator instance or class, but ok I can change it to be an instance. |
Not sure why that would be required. I would suggest to just use the new operator. |
@bantu I've just deleted "naturalsortcomparator.php" which was the wrong class, the correct one is "naturalsort.php" I don't want to "new" the class multiple times in the same php call. @icewind1991 any comments about this pattern ? |
Okay. I see. Not sure whether it would be possible to have this in the container where it would be ensured that it is only created once. |
if (aNum == aa[x] && bNum == bb[x]) { | ||
return aNum - bNum; | ||
} else { | ||
return aa[x].localeCompare(bb[x], 'en'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't the locale be determined from the used language?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally yes, but in our case we want it to match the sorting of PHP which locale is always set to English.
From my test cases it worked quite well even for German umlauts and Chinese characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment explaining that?
We have the server container for that ( |
A new inspection was created. |
Rebased onto master, should run on Travis now. |
@DeepDiver1975 @MorrisJobke segfault on Jenkins. |
@PVince81 They run in parallel. Travis is just too slow yet. (just 5 jobs at the same time isn't enough to manage our workload) |
🚀 Test Passed. 🚀 |
Please review, unit tests finally pass! |
👎 This doesn't fix the order after the reload. |
Hmm strange, it should. Will try again. |
@MorrisJobke works for me. Please provide more details and make sure you have the latest version of this branch that was rebased multiple times already. Thanks. |
I just checked out the latest change, and tried with firefox and chromium: https://cloud.morrisjobke.de/public.php?service=files&t=cac3ab8f16cca86e1e9b6bd4f09b3a3b Both sort in the wrong way after the refresh. Are any special PHP modules required? |
I just enabled the intl module -.- I will try again dum-di-dum |
Nice one: Works excellent now 👍 |
@jancborchardt have you had a look at this ? This needs another reviewer @th3fallen @LukasReschke |
👍 |
Fixed JS sort comparator to be consistent between JS and PHP
used for the user list
String.localeCompare()
Fixes for #7059, #3804