update name sorting

This commit is contained in:
Luke Pulverenti 2016-03-24 16:27:44 -04:00
parent 7aa85fc8dd
commit cc20c8d7d6
7 changed files with 102 additions and 33 deletions

View File

@ -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<Tuple<StringBuilder,bool>> GetSortChunks(string s1)
{
var list = new List<Tuple<StringBuilder, bool>>();
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<StringBuilder, bool>(thisChunk, isNumeric));
}
return list;
}
/// <summary>
/// This is just a helper for convenience
/// </summary>
@ -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;
}

View File

@ -331,6 +331,7 @@
<Compile Include="Security\IEncryptionManager.cs" />
<Compile Include="Session\AuthenticationRequest.cs" />
<Compile Include="Social\ISharingManager.cs" />
<Compile Include="Sorting\SortHelper.cs" />
<Compile Include="Subtitles\ISubtitleManager.cs" />
<Compile Include="Subtitles\ISubtitleProvider.cs" />
<Compile Include="Providers\ItemIdentifier.cs" />

View File

@ -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;
}
}
}

View File

@ -79,7 +79,7 @@ namespace MediaBrowser.Server.Implementations.Persistence
private IDbCommand _updateInheritedRatingCommand;
private const int LatestSchemaVersion = 49;
private const int LatestSchemaVersion = 50;
/// <summary>
/// Initializes a new instance of the <see cref="SqliteItemRepository"/> class.

View File

@ -1,30 +1,11 @@
using System.Collections.Generic;
using System.Text;
using MediaBrowser.Controller.Sorting;
namespace MediaBrowser.Server.Implementations.Sorting
{
public class AlphanumComparator : IComparer<string>
{
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++;

View File

@ -16,7 +16,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>System.Int32.</returns>
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)

View File

@ -18,12 +18,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>System.Int32.</returns>
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);
}
/// <summary>