-
Notifications
You must be signed in to change notification settings - Fork 8
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
optimize the natural sorting: improve sorting and performance #83
Conversation
Can you give some before and after examples of how a set of files will be sorted as a result of this change? |
This is the method 4 mentioned in there: http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua The following is an example of the difference between the two sorting methods. In addition, the original sorting method has the following problems:
|
file-browser.lua
Outdated
table.sort(t, function(a, b) return a.type:sub(1, 1) .. (a.label or a.name):lower():gsub("%.?%d+", padnum) .. ("%3d"):format(#b) | ||
< b.type:sub(1, 1) .. (b.label or b.name):lower():gsub("%.?%d+", padnum) .. ("%3d"):format(#a) end) |
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.
What is the ("%3d"):format(#b)
and ("%3d"):format(#a)
doing?
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.
In case the rest of the comparison is the same, it'll sort them by string length.
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.
I believe in this case a and b are both item tables though? The length operator should not work. But I assume the idea is that if extra digits are inserted for the comparison then we want to prefer the item which had fewer digits initially?
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 exactly, that seems to be a bug. It should use the length of (label or name)
instead.
There have been performance and sorting improvements on this algorithm since this was posted. |
@dyphire do you have a response to this? |
I agree with @christoph-heinrich that it is necessary to improve the implementation of the algorithm. But I'm not sure how to handle the sorting of folders and files in the new algorithm. |
I didn't test it, but I think something like that should work. tuples[i] = {f.type:sub(1, 1) .. ((f.label or f.name)):lower():gsub("0*(%d+)%.?(%d*)", padnum), f} |
Yes, You're right. It's works. |
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.
Looks good. Next time send me a ping when you want me to do a re-review. I had no idea that you had pushed an updated commit until today.
--the number format functionality was proposed by github user twophyro, and was presumably taken | ||
--from here: http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua |
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.
Do we need to change this comment? Perhaps cite autoload.lua
instead? Or does autoload.lua
also use an algorithm from that page?
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, autoload.lua
also use an algorithm from that page: mpv-player/mpv#10779
@dyphire I'm happy to merge this if you are. |
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.
Actually wait, one change.
It has been ready since the last push |
Original sorting method doesn't correctly sort numbers with decimal points. Ref: https://github.com/mpv-player/mpv/blob/fd2f1a6f9af5087ca8e6e878176db6308fd4b45a/TOOLS/lua/autoload.lua#L100-L115
Original sorting method doesn't correctly sort numbers with decimal points.