From cc20c8d7d6c8b46e5026f2ad8d73473dec3fe9d8 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 24 Mar 2016 16:27:44 -0400 Subject: update name sorting --- MediaBrowser.Controller/Entities/BaseItem.cs | 67 +++++++++++++++++++++- .../MediaBrowser.Controller.csproj | 1 + MediaBrowser.Controller/Sorting/SortHelper.cs | 31 ++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 MediaBrowser.Controller/Sorting/SortHelper.cs (limited to 'MediaBrowser.Controller') 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; + } + } +} -- cgit v1.2.3