mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
70 lines
2.3 KiB
C#
70 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using API.Services.Tasks.Scanner.Parser;
|
|
|
|
namespace API.Comparators;
|
|
|
|
#nullable enable
|
|
|
|
/// <summary>
|
|
/// Sorts chapters based on their Number. Uses natural ordering of doubles.
|
|
/// </summary>
|
|
public class ChapterSortComparer : IComparer<double>
|
|
{
|
|
/// <summary>
|
|
/// Normal sort for 2 doubles. 0 always comes last
|
|
/// </summary>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <returns></returns>
|
|
public int Compare(double x, double y)
|
|
{
|
|
if (x == Parser.DefaultChapterNumber && y == Parser.DefaultChapterNumber) return 0;
|
|
// if x is 0, it comes second
|
|
if (x == Parser.DefaultChapterNumber) return 1;
|
|
// if y is 0, it comes second
|
|
if (y == Parser.DefaultChapterNumber) return -1;
|
|
|
|
return x.CompareTo(y);
|
|
}
|
|
|
|
public static readonly ChapterSortComparer Default = new ChapterSortComparer();
|
|
}
|
|
|
|
/// <summary>
|
|
/// This is a special case comparer used exclusively for sorting chapters within a single Volume for reading order.
|
|
/// <example>
|
|
/// Volume 10 has "Series - Vol 10" and "Series - Vol 10 Chapter 81". In this case, for reading order, the order is Vol 10, Vol 10 Chapter 81.
|
|
/// This is represented by Chapter 0, Chapter 81.
|
|
/// </example>
|
|
/// </summary>
|
|
public class ChapterSortComparerZeroFirst : IComparer<double>
|
|
{
|
|
public int Compare(double x, double y)
|
|
{
|
|
if (x == Parser.DefaultChapterNumber && y == Parser.DefaultChapterNumber) return 0;
|
|
// if x is 0, it comes first
|
|
if (x == Parser.DefaultChapterNumber) return -1;
|
|
// if y is 0, it comes first
|
|
if (y == Parser.DefaultChapterNumber) return 1;
|
|
|
|
return x.CompareTo(y);
|
|
}
|
|
|
|
public static readonly ChapterSortComparerZeroFirst Default = new ChapterSortComparerZeroFirst();
|
|
}
|
|
|
|
public class SortComparerZeroLast : IComparer<double>
|
|
{
|
|
public int Compare(double x, double y)
|
|
{
|
|
if (x == Parser.DefaultChapterNumber && y == Parser.DefaultChapterNumber) return 0;
|
|
// if x is 0, it comes last
|
|
if (x == Parser.DefaultChapterNumber) return 1;
|
|
// if y is 0, it comes last
|
|
if (y == Parser.DefaultChapterNumber) return -1;
|
|
|
|
return x.CompareTo(y);
|
|
}
|
|
public static readonly SortComparerZeroLast Default = new SortComparerZeroLast();
|
|
}
|