ex-int-02-meet-the-teacher/MeetTheTeacher/Model/Comparison/ByNameComparer.cs
2025-04-24 23:34:37 +02:00

27 lines
997 B
C#

namespace MeetTheTeacher.Model.Comparison;
/// <summary>
/// A comparer which allows sorting <see cref="Teacher" /> instances by their name in either ascending or descending
/// order
/// </summary>
/// <inheritdoc cref="ComparerBase" />
/// <inheritdoc cref="IComparer{Teacher}"/>
public sealed class ByNameComparer : ComparerBase, IComparer<Teacher>
{
/// <summary>
/// Creates a new instance of <see cref="ByNameComparer" /> configured to sort in ascending or descending order
/// </summary>
/// <param name="ascending">Flag indicating if items should be sorted in ascending order; descending if false</param>
public ByNameComparer(bool ascending) : base(ascending) { }
public int Compare(Teacher? x, Teacher? y)
{
int nullCheck = CheckNull(x, y);
if (nullCheck != 2)
{
return nullCheck;
}
return ApplyCorrectDirection(string.Compare(x!.Name, y!.Name, StringComparison.Ordinal));
}
}