diff options
| author | Eric Reed <ebr@mediabrowser3.com> | 2013-12-09 09:24:21 -0500 |
|---|---|---|
| committer | Eric Reed <ebr@mediabrowser3.com> | 2013-12-09 09:24:21 -0500 |
| commit | 83c4d75b1484ad7e7aa99a1abba8b7903391ec95 (patch) | |
| tree | 00d426fd355a6bb0a0d1ac5276034691349b5f59 /MediaBrowser.Server.Implementations/Sorting | |
| parent | bec5a4f561974941ed07638d3e1fb8a974ac0cc5 (diff) | |
| parent | 41ed3c15ffee7430b0718114bc7732f354198fb7 (diff) | |
Merge branch 'master' of https://github.com/MediaBrowser/MediaBrowser
Diffstat (limited to 'MediaBrowser.Server.Implementations/Sorting')
5 files changed, 126 insertions, 6 deletions
diff --git a/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs index 76971342a..d6b78bd84 100644 --- a/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs @@ -62,7 +62,7 @@ namespace MediaBrowser.Server.Implementations.Sorting return CompareEpisodes(x, y); } - if (!isXSpecial && isYSpecial) + if (!isXSpecial) { return CompareEpisodeToSpecial(x, y); } @@ -87,8 +87,17 @@ namespace MediaBrowser.Server.Implementations.Sorting // Add 1 to to non-specials to account for AirsBeforeEpisodeNumber var xEpisode = x.IndexNumber ?? -1; xEpisode++; + var yEpisode = y.AirsBeforeEpisodeNumber ?? 10000; + // Sometimes they'll both have a value. + // For example AirsAfterSeasonNumber=1, AirsBeforeSeasonNumber=2, AirsBeforeEpisodeNumber=1 + // The episode should be displayed at the end of season 1 + if (y.AirsAfterSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.Value > y.AirsAfterSeasonNumber.Value) + { + yEpisode = 10000; + } + return xEpisode.CompareTo(yEpisode); } diff --git a/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs b/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs new file mode 100644 index 000000000..3fbb01f77 --- /dev/null +++ b/MediaBrowser.Server.Implementations/Sorting/AlphanumComparator.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Text; + +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) + { + return 0; + } + + int thisMarker = 0, thisNumericChunk = 0; + int thatMarker = 0, thatNumericChunk = 0; + + while ((thisMarker < s1.Length) || (thatMarker < s2.Length)) + { + if (thisMarker >= s1.Length) + { + return -1; + } + else if (thatMarker >= s2.Length) + { + return 1; + } + char thisCh = s1[thisMarker]; + char thatCh = s2[thatMarker]; + + StringBuilder thisChunk = new StringBuilder(); + StringBuilder thatChunk = new StringBuilder(); + + while ((thisMarker < s1.Length) && (thisChunk.Length == 0 || InChunk(thisCh, thisChunk[0]))) + { + thisChunk.Append(thisCh); + thisMarker++; + + if (thisMarker < s1.Length) + { + thisCh = s1[thisMarker]; + } + } + + while ((thatMarker < s2.Length) && (thatChunk.Length == 0 || InChunk(thatCh, thatChunk[0]))) + { + thatChunk.Append(thatCh); + thatMarker++; + + if (thatMarker < s2.Length) + { + thatCh = s2[thatMarker]; + } + } + + int result = 0; + // If both chunks contain numeric characters, sort them numerically + if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0])) + { + thisNumericChunk = Convert.ToInt32(thisChunk.ToString()); + thatNumericChunk = Convert.ToInt32(thatChunk.ToString()); + + if (thisNumericChunk < thatNumericChunk) + { + result = -1; + } + + if (thisNumericChunk > thatNumericChunk) + { + result = 1; + } + } + else + { + result = thisChunk.ToString().CompareTo(thatChunk.ToString()); + } + + if (result != 0) + { + return result; + } + } + + return 0; + } + + public int Compare(string x, string y) + { + return CompareValues(x, y); + } + } +} diff --git a/MediaBrowser.Server.Implementations/Sorting/NameComparer.cs b/MediaBrowser.Server.Implementations/Sorting/NameComparer.cs index 49f86c485..83b1b2d16 100644 --- a/MediaBrowser.Server.Implementations/Sorting/NameComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/NameComparer.cs @@ -1,7 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Querying; -using System; namespace MediaBrowser.Server.Implementations.Sorting { @@ -18,7 +17,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// <returns>System.Int32.</returns> public int Compare(BaseItem x, BaseItem y) { - return string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase); + return AlphanumComparator.CompareValues(x.Name, y.Name); } /// <summary> diff --git a/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs index 4efc3218b..09612a49c 100644 --- a/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/SeriesSortNameComparer.cs @@ -16,7 +16,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// <returns>System.Int32.</returns> public int Compare(BaseItem x, BaseItem y) { - return string.Compare(GetValue(x), GetValue(y), StringComparison.CurrentCultureIgnoreCase); + return AlphanumComparator.CompareValues(GetValue(x), GetValue(y)); } private string GetValue(BaseItem item) diff --git a/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs index 873753a2b..e635cfbe5 100644 --- a/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs +++ b/MediaBrowser.Server.Implementations/Sorting/SortNameComparer.cs @@ -1,7 +1,6 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Sorting; using MediaBrowser.Model.Querying; -using System; namespace MediaBrowser.Server.Implementations.Sorting { @@ -18,7 +17,7 @@ namespace MediaBrowser.Server.Implementations.Sorting /// <returns>System.Int32.</returns> public int Compare(BaseItem x, BaseItem y) { - return string.Compare(x.SortName, y.SortName, StringComparison.CurrentCultureIgnoreCase); + return AlphanumComparator.CompareValues(x.SortName, y.SortName); } /// <summary> |
