From cc20c8d7d6c8b46e5026f2ad8d73473dec3fe9d8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 24 Mar 2016 16:27:44 -0400 Subject: [PATCH] update name sorting --- MediaBrowser.Controller/Entities/BaseItem.cs | 67 ++++++++++++++++++- .../MediaBrowser.Controller.csproj | 1 + MediaBrowser.Controller/Sorting/SortHelper.cs | 31 +++++++++ .../Persistence/SqliteItemRepository.cs | 2 +- .../Sorting/AlphanumComparator.cs | 25 +------ .../Sorting/SeriesSortNameComparer.cs | 2 +- .../Sorting/SortNameComparer.cs | 7 +- 7 files changed, 102 insertions(+), 33 deletions(-) create mode 100644 MediaBrowser.Controller/Sorting/SortHelper.cs diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index c9fa1cf79..995480535 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -20,9 +20,11 @@ using System.Globalization; using System.IO; using System.Linq; using System.Runtime.Serialization; +using System.Text; using System.Threading; using System.Threading.Tasks; using CommonIO; +using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.LiveTv; namespace MediaBrowser.Controller.Entities @@ -299,6 +301,40 @@ namespace MediaBrowser.Controller.Entities } } + private List> GetSortChunks(string s1) + { + var list = new List>(); + + int thisMarker = 0, thisNumericChunk = 0; + + while ((thisMarker < s1.Length)) + { + if (thisMarker >= s1.Length) + { + break; + } + char thisCh = s1[thisMarker]; + + StringBuilder thisChunk = new StringBuilder(); + + while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0]))) + { + thisChunk.Append(thisCh); + thisMarker++; + + if (thisMarker < s1.Length) + { + thisCh = s1[thisMarker]; + } + } + + var isNumeric = thisChunk.Length > 0 && char.IsDigit(thisChunk[0]); + list.Add(new Tuple(thisChunk, isNumeric)); + } + + return list; + } + /// /// This is just a helper for convenience /// @@ -484,7 +520,7 @@ namespace MediaBrowser.Controller.Entities { return System.IO.Path.Combine(basePath, "channels", ChannelId, Id.ToString("N")); } - + var idString = Id.ToString("N"); basePath = System.IO.Path.Combine(basePath, "library"); @@ -527,7 +563,32 @@ namespace MediaBrowser.Controller.Entities sortable = sortable.Remove(sortable.Length - (searchLower.Length + 1)); } } - return sortable; + return ModifySortChunks(sortable); + } + + private string ModifySortChunks(string name) + { + var chunks = GetSortChunks(name); + + var builder = new StringBuilder(); + + foreach (var chunk in chunks) + { + var chunkBuilder = chunk.Item1; + + // This chunk is numeric + if (chunk.Item2) + { + while (chunkBuilder.Length < 10) + { + chunkBuilder.Insert(0, '0'); + } + } + + builder.Append(chunkBuilder); + } + Logger.Debug("ModifySortChunks Start: {0} End: {1}", name, builder.ToString()); + return builder.ToString(); } [IgnoreDataMember] @@ -1146,7 +1207,7 @@ namespace MediaBrowser.Controller.Entities { return false; } - + return ConfigurationManager.Configuration.SaveLocalMeta; } diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index 8941e5384..6f429ed2f 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -331,6 +331,7 @@ + diff --git a/MediaBrowser.Controller/Sorting/SortHelper.cs b/MediaBrowser.Controller/Sorting/SortHelper.cs new file mode 100644 index 000000000..95a3c26c4 --- /dev/null +++ b/MediaBrowser.Controller/Sorting/SortHelper.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.Sorting +{ + public static class SortHelper + { + private enum ChunkType { Alphanumeric, Numeric }; + + public static bool InChunk(char ch, char otherCh) + { + var type = ChunkType.Alphanumeric; + + if (char.IsDigit(otherCh)) + { + type = ChunkType.Numeric; + } + + if ((type == ChunkType.Alphanumeric && char.IsDigit(ch)) + || (type == ChunkType.Numeric && !char.IsDigit(ch))) + { + return false; + } + + return true; + } + } +} diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs index 19185e965..8a679eb05 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs @@ -79,7 +79,7 @@ namespace MediaBrowser.Server.Implementations.Persistence private IDbCommand _updateInheritedRatingCommand; - private const int LatestSchemaVersion = 49; + private const int LatestSchemaVersion = 50; /// /// Initializes a new instance of the class. diff --git a/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs b/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs index b76bf0a9c..232bdb3b5 100644 --- a/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs +++ b/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs @@ -1,30 +1,11 @@ using System.Collections.Generic; using System.Text; +using MediaBrowser.Controller.Sorting; namespace MediaBrowser.Server.Implementations.Sorting { public class AlphanumComparator : IComparer { - private enum ChunkType { Alphanumeric, Numeric }; - - private static bool InChunk(char ch, char otherCh) - { - var type = ChunkType.Alphanumeric; - - if (char.IsDigit(otherCh)) - { - type = ChunkType.Numeric; - } - - if ((type == ChunkType.Alphanumeric && char.IsDigit(ch)) - || (type == ChunkType.Numeric && !char.IsDigit(ch))) - { - return false; - } - - return true; - } - public static int CompareValues(string s1, string s2) { if (s1 == null || s2 == null) @@ -51,7 +32,7 @@ namespace MediaBrowser.Server.Implementations.Sorting StringBuilder thisChunk = new StringBuilder(); StringBuilder thatChunk = new StringBuilder(); - while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0]))) + while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || SortHelper.InChunk(thisCh, thisChunk[0]))) { thisChunk.Append(thisCh); thisMarker++; @@ -62,7 +43,7 @@ namespace MediaBrowser.Server.Implementations.Sorting } } - while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0]))) + while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || SortHelper.InChunk(thatCh, thatChunk[0]))) { thatChunk.Append(thatCh); thatMarker++; diff --git a/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs index 09612a49c..4efc3218b 100644 --- a/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs @@ -16,7 +16,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// System.Int32. public int Compare(BaseItem x, BaseItem y) { - return AlphanumComparator.CompareValues(GetValue(x), GetValue(y)); + return string.Compare(GetValue(x), GetValue(y), StringComparison.CurrentCultureIgnoreCase); } private string GetValue(BaseItem item) diff --git a/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs index 389b21ba7..873753a2b 100644 --- a/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs @@ -18,12 +18,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// System.Int32. public int Compare(BaseItem x, BaseItem y) { - if (!x.EnableAlphaNumericSorting || !y.EnableAlphaNumericSorting) - { - return string.Compare(x.SortName, y.SortName, StringComparison.CurrentCultureIgnoreCase); - } - - return AlphanumComparator.CompareValues(x.SortName, y.SortName); + return string.Compare(x.SortName, y.SortName, StringComparison.CurrentCultureIgnoreCase); } ///