Bug Sorting By Multiple Fields Needs New Approach
#1
It seems to be that the way Kodi currently does sorting by multiple fields is fundamentally flawed. A couple of users reported some odd inconsistencies in sorted results, and looking into it I can understand why Kodi is unable to produce consistent sorting. That is very unexpected from such a mature component of Kodi.

For those that don't know it sorting is not done in the databases using ORDER BY statements in SQL, but as explained by Montelesse in the big PR that implmeneted it back in 2012 (PR1047), so that smart playlists and default nodes can use the same sorting it is done in memory on the linked list of file items that was populated from the results dataset.

That is all well and good, but item order in the list is determined by combining the various fields of the sort into a single string value and comparing that alphanumerically. That results in inconsistent ordering.

The issue is best shown with an example from the music library, although the same sorting is also used by video. Say you have 3 compliation albums by "Various Artists" called "Back To The Old Skool", "Back To The Old Skool 2", "Back To The Old Skool Drum N Bass", and you view them from the albums node.

Sorting albums "by artist" is a sort by Artist and Album title, it uses a string <artist name> <album title>.
In our example that gives sorted strings

"Various Artists Back To The Old Skool"
"Various Artists Back To The Old Skool 2"
"Various Artists Back To The Old Skool Drum N Bass"

- the albums appear in the expected order.

Sorting albums "by album" is a sort by Album title and Artist, it uses a string <album title> <artist name>.
In our example that gives sorted strings

"Back To The Old Skool 2 Various Artists"
"Back To The Old Skool Drum N Bass Various Artists"
"Back To The Old Skool Various Artists"

- the resulting album order is unexpected.
How the albums get suffled with depend on the artist name, and how similar the titles. It is right to sort by title and artist, you could be looking at a list of albums and some have the same title but different artists e.g. "Greatest Hits". But the secondary sort order should not casue the albums to appear in the wrong order.

Multiple field sorting need the fields to considered separately, accurate item comparison should not be attempted by concatening these to a single string.

It should be possible to correct this by reworking SortUtils.cpp, it has the separate field values for the items it sorts, but just doesn't use them when comparing items. But since it is such a fundamental part of how the libraries work, I didn't want to rework anything without some input from others.

Perhaps there was a good reason for trying to sort things like this? Maybe @Montelesse could comment, although I know a lot happens in 4 years, and he is busy with other things. I guess I am looking at least for a sanity check, as I'm so surprized that sorting has been so flawed for so long unnoticed.
Reply
#2
I didn't even know we support orderning by multiple fields... Is this an implicit thing like "sort by year (and by title/label within the same year) because the SortingDescription doesn't allow specifying multiple sort methods IIRC.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply
#3
(2016-12-08, 16:08)Montellese Wrote: I didn't even know we support orderning by multiple fields... Is this an implicit thing like "sort by year (and by title/label within the same year) because the SortingDescription doesn't allow specifying multiple sort methods IIRC.

Mostly it is implicit yes. So say when sorting "by album" we actually want to sort by album title and then artist, that way all the albums with the same title e.g. "Greatest Hits" have a secondary ordering, or as in the example you give.

But also we have sorting "by artist and year" which is 2 fields explicitily, that implicitiy uses ablum title as a third order should an artist have release more than one album in a year.

But I would value your input on changes to sortuils since you did all the original work.
Reply
#4
TBH most of the work I did was refactoring stuff out of CFileItem into a helper class. I didn't really change the way sorting works in the end. I just made it easier to provide all the necessary details for sorting. Since SortUtils already knows the different fields it should sort by you could probably be able to rework it to operate on multiple strings instead of a concatenated one.
Always read the online manual (wiki), FAQ (wiki) and search the forum before posting.
Do not e-mail Team Kodi members directly asking for support. Read/follow the forum rules (wiki).
Please read the pages on troubleshooting (wiki) and bug reporting (wiki) before reporting issues.
Reply

Logout Mark Read Team Forum Stats Members Help
Sorting By Multiple Fields Needs New Approach0