Repository: jellyfin/jellyfin

Issue:
Sort versions in ascending order when resolution is the same
Change movie version to sort in ascending order when resolution is the same

**Issues**
Fixes #12618

Versions are sorted in reverse alphabetical order when filename includes resolution
When multiple files share the same resolution in their filenames, they are sorted in reverse alphabetical order.
https://github.com/jellyfin/jellyfin/blob/54f663b0f3c4a9cc5a4f44d1afcb6e1de03c0503/Emby.Naming/Video/VideoListResolver.cs#L144
![Screenshot from 2024-09-09 10-50-18](https://github.com/user-attachments/assets/a81fb785-3521-4b8d-8964-41e516c22220)

<br><br>

Since the resolutions are sorted based on the MediaInfo, it should be sufficient to change `OrderByDescending` to just `OrderBy` .
```c#
videos.InsertRange(0, group.OrderBy(x => x.Files[0].FileNameWithoutExtension.ToString(), new AlphanumericComparator())); 
``` 
<br>
Making this change resolves the issue, and resolutions are  sorted alphabetical, with higher resolutions listed first, regardless of whether the resolution is included in the filename.

![Screenshot from 2024-09-09 10-46-21](https://github.com/user-attachments/assets/db23c97a-0895-404c-b172-07a0d475341f)

Server: 10.9.11





Hints:

Please provide the media info of all 3 files 
Those were all the same file, just renamed. I'll also upload with different resolutions in a bit. 

```
General
Unique ID                                : 34727860135091814094139690058492232674 (0x1A2057B2EBC11E00F924498B258D43E2)
Complete name                            : /media/max/testmedia/Multiple Versions/Big Buck Bunny (2008)/Big Buck Bunny (2008) - 2160p Director's cut.mkv
Format                                   : Matroska
Format version                           : Version 4
File size                                : 604 MiB
Duration                                 : 10 min 34 s
Overall bit rate                         : 7 980 kb/s
Frame rate                               : 30.000 FPS
Movie name                               : Big Buck Bunny, Sunflower version
Encoded date                             : 2024-06-23 14:51:05 UTC
Writing application                      : mkvmerge v85.0 ('Shame For You') 64-bit
Writing library                          : libebml v1.4.5 + libmatroska v1.7.1
Comment                                  : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 10 min 34 s
Bit rate                                 : 7 500 kb/s
Width                                    : 3 840 pixels
Height                                   : 2 160 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 30.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.030
Stream size                              : 567 MiB (94%)
Writing library                          : x264 core 120
```
Example with different resolution, current behavior:
![Screenshot from 2024-09-09 14-58-44](https://github.com/user-attachments/assets/5e48ddc0-7e80-4028-8c6e-681798ab531d)
<br><br>
With proposed changed in code:
![Screenshot from 2024-09-09 14-58-39](https://github.com/user-attachments/assets/105a85d7-c34e-4f82-b0c6-92847ff75404)
<br>
Only two files tested here one 2160p media info above and 1080p info below. All versions are just copies.

```
General
Complete name                            : /media/max/testmedia/Multiple Versions/Big Buck Bunny (2008)/Big Buck Bunny (2008) - Alpha teneightyp.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom (isom/avc1)
File size                                : 263 MiB
Duration                                 : 10 min 34 s
Overall bit rate                         : 3 481 kb/s
Frame rate                               : 30.000 FPS
Movie name                               : Big Buck Bunny, Sunflower version
Performer                                : Blender Foundation 2008, Janus Bager Kristensen 2013
Composer                                 : Sacha Goedegebure
Genre                                    : Animation
Encoded date                             : 2013-12-16 17:44:39 UTC
Tagged date                              : 2013-12-16 17:44:39 UTC
Comment                                  : Creative Commons Attribution 3.0 - http://bbb3d.renderfarming.net
com                                      : Jan Morgenstern

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4.1
Format settings                          : CABAC / 4 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 4 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 10 min 34 s
Bit rate                                 : 3 000 kb/s
Maximum bit rate                         : 16.7 Mb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 30.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.048
Stream size                              : 227 MiB (86%)
Writing library                          : x264 core 115
```



Patch:
diff --git a/Emby.Naming/Video/VideoListResolver.cs b/Emby.Naming/Video/VideoListResolver.cs
index 51f29cf0887..12bc22a6ac0 100644
--- a/Emby.Naming/Video/VideoListResolver.cs
+++ b/Emby.Naming/Video/VideoListResolver.cs
@@ -141,7 +141,9 @@ private static List<VideoInfo> GetVideosGroupedByVersion(List<VideoInfo> videos,
                 {
                     if (group.Key)
                     {
-                        videos.InsertRange(0, group.OrderByDescending(x => x.Files[0].FileNameWithoutExtension.ToString(), new AlphanumericComparator()));
+                        videos.InsertRange(0, group
+                            .OrderByDescending(x => ResolutionRegex().Match(x.Files[0].FileNameWithoutExtension.ToString()).Value, new AlphanumericComparator())
+                            .ThenBy(x => x.Files[0].FileNameWithoutExtension.ToString(), new AlphanumericComparator()));
                     }
                     else
                     {


Test patch:
diff --git a/tests/Jellyfin.Naming.Tests/Video/MultiVersionTests.cs b/tests/Jellyfin.Naming.Tests/Video/MultiVersionTests.cs
index 183ec898485..3005a4416c0 100644
--- a/tests/Jellyfin.Naming.Tests/Video/MultiVersionTests.cs
+++ b/tests/Jellyfin.Naming.Tests/Video/MultiVersionTests.cs
@@ -356,6 +356,45 @@ public void TestMultiVersion12()
             Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - Theatrical Release.mkv", result[0].AlternateVersions[4].Path);
         }
 
+        [Fact]
+        public void TestMultiVersion13()
+        {
+            var files = new[]
+            {
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - Theatrical Release.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - Directors Cut.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 2160p.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Directors Cut.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 2160p Remux.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Theatrical Release.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 720p.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Remux.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 720p Directors Cut.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p High Bitrate.mkv",
+                "/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016).mkv",
+            };
+
+            var result = VideoListResolver.Resolve(
+                files.Select(i => VideoResolver.Resolve(i, false, _namingOptions)).OfType<VideoFileInfo>().ToList(),
+                _namingOptions).ToList();
+
+            Assert.Single(result);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016).mkv", result[0].Files[0].Path);
+            Assert.Equal(11, result[0].AlternateVersions.Count);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 2160p.mkv", result[0].AlternateVersions[0].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 2160p Remux.mkv", result[0].AlternateVersions[1].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p.mkv", result[0].AlternateVersions[2].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Directors Cut.mkv", result[0].AlternateVersions[3].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p High Bitrate.mkv", result[0].AlternateVersions[4].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Remux.mkv", result[0].AlternateVersions[5].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 1080p Theatrical Release.mkv", result[0].AlternateVersions[6].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 720p.mkv", result[0].AlternateVersions[7].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - 720p Directors Cut.mkv", result[0].AlternateVersions[8].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - Directors Cut.mkv", result[0].AlternateVersions[9].Path);
+            Assert.Equal("/movies/X-Men Apocalypse (2016)/X-Men Apocalypse (2016) - Theatrical Release.mkv", result[0].AlternateVersions[10].Path);
+        }
+
         [Fact]
         public void Resolve_GivenFolderNameWithBracketsAndHyphens_GroupsBasedOnFolderName()
         {