mpv
mpv copied to clipboard
TOOLS/lua/autoload: further optimize the natural sorting
Commits https://github.com/mpv-player/mpv/commit/257dbdf06feb366f9eb1f96ceb7026c9365dddaa uses a algorithm of splitting strings into a table of number and string values to achieve natural sorting. This approach works well in most cases, but it does not work well in some specific scenarios. Now let's implement natural sorting with a stronger algorithm.
Ref: https://github.com/jonniek/mpv-playlistmanager/commit/3cf323f3c419af77f4141119283c1ec2911491c5 Closes https://github.com/mpv-player/mpv/issues/8969
Use 4 spaces for indentation instead of 2.
Good touch, it's my bad.
I think it would be better if the function actually sorted the table rather then just comparing two strings like in the original.
Do you mean the original implementation of this function of alphanumsort in Alphanum sorting for humans in Lua? The current approach seems to be good enough for me.
Do you mean the original implementation of this function of alphanumsort in Alphanum sorting for humans in Lua? The current approach seems to be good enough for me.
Yes, because the function is called alphanumsort and also this way the function padnum()
gets created every time two strings get compared.
Yes, because the function is called alphanumsort and also this way the function
padnum()
gets created every time two strings get compared.
It seems reasonable, done. Thanks for your advice.
Still duplicates entries (4bc6686 did not fix it for me) and fails to sort properly. https://i.imgur.com/5goTnwP.png The index is supposed to go up by 2 for each video.
EDIT: Found the issue, the script was being loaded twice.
Still duplicates entries (4bc6686 did not fix it for me) and fails to sort properly. https://i.imgur.com/5goTnwP.png
EDIT: The index is supposed to go up by 2 for each video.
Your issue has nothing to do with this pr. That is the uncontrollable behavior after loading duplicate files due to unknown reasons.
The following is the sorting result of similar files in your screenshot tested with this pr:
It is in line with expectations. Let's go back to the original issue https://github.com/mpv-player/mpv/issues/8575 and discuss repeated loading.
Closes https://github.com/mpv-player/mpv/issues/8969
we cannot really fix it because
-
When playing a movie in a directory of movies, autoload.lua loads all movies into the playlist in the same alphabetical order as File Explorer, Total Command, zsh in WSL, etc.
- "The algorithm itself is not explained"
I also tried to search for the Finder's sorting algorithm. Unfortunately nothing valuable I could found neither.
Your issue has nothing to do with this pr. That is the uncontrollable behavior after loading duplicate files due to unknown reasons. Let's go back to the original issue #8575 and discuss repeated loading.
Even this? https://i.imgur.com/eszbCDr.png Are you saying that when it loads a file twice in the playlist it just places them randomly and doesn't apply sorting because it's already assumed to be completed?
EDIT: It was loading the script twice.
Even this? https://i.imgur.com/eszbCDr.png Are you saying that when it loads a file twice in the playlist it just places them randomly and doesn't apply sorting because it's already assumed to be completed?
Indeed, this script of autoload.lua only recurses and sorts files when they are loaded a file. After that, if the playlist changes, it will not be sorted again.
This is beyond the expected function of this script because it is not a playlist management script. But you can use this script of playlistmanager.lua to implement reorder the playlist when it changes.
Closes #8969
we cannot really fix it because
When playing a movie in a directory of movies, autoload.lua loads all movies into the playlist in the same alphabetical order as File Explorer, Total Command, zsh in WSL, etc.
- "The algorithm itself is not explained"
I also tried to search for the Finder's sorting algorithm. Unfortunately nothing valuable I could found neither.
You are right, although the new natural sorting algorithm can correctly handle the wrong sorting behavior mentioned in that issue. But we still haven't implemented the sorting algorithm that is completely consistent with the File Explorer.
This is actually difficult to achieve because "The algorithm itself is not explained".
@christoph-heinrich does further optimizes the performance of string sorting. Thanks for the contribution.
I want two files to be loaded in the following order, just like how Windows Explorer and MPC-HC sort them: EXAMPLE.mkv EXAMPLE-2.mkv
But instead autoload loads them in reverse order: EXAMPLE-2.mkv EXAMPLE.mkv
How to fix it?
@christoph-heinrich Why you deleted your last answer? Well, I'm new to MPV, and just installed uosc. What should I do with this utils.lua to achieve what I want?
Why you deleted your last answer?
Because I realized that uosc only sorts the first character specially and thus wouldn't apply to your example. We would have to do that for each character in the string.
Fwiw the file manager dolphin sorts those files in the same way autoload does.
Because I realized that uosc only sorts the first character specially and thus wouldn't apply to your example.
So there's no way to make MPV (or autload, or uosc) sort files like Windows File Explorer does?
So there's no way to make MPV (or autload, or uosc) sort files like Windows File Explorer does?
Not without changing the sorting algorithm yourself. Maybe you can find it in an already existing script.
The code from uosc is probably already pretty close to the right solution, but the function passed to the table.sort
has to iterate over individual characters and check the symbol order.
@vitormathews this page on lua string sorting may be of use to you.
Would it be possible to change the Windows sorting logic? As I'm new to programming and that kind of thing, could you tell me the name of the sorting rule used by uosc/autoload and by WIndows? So that I can more easily search for a way to get Windows to work like uosc's/autoload's.
As I mentioned here https://github.com/mpv-player/mpv/pull/10779#issuecomment-1290831496 , it's impossible.