< Summary

Information
Class: MyNet.Observable.Collections.Sorting.SortingComparer<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Sorting/SortingComparer.cs
Tag: 323_28699572109
Line coverage
93%
Covered lines: 15
Uncovered lines: 1
Coverable lines: 16
Total lines: 65
Line coverage: 93.7%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%44100%
Compare(...)75%1212100%
Compare(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Sorting/SortingComparer.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SortingComparer.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections;
 9using System.Collections.Generic;
 10using System.ComponentModel;
 11using System.Linq;
 12
 13namespace MyNet.Observable.Collections.Sorting;
 14
 15/// <summary>
 16/// Implements a comparer that compares objects of type T based on multiple sorting properties defined in an array of <s
 17/// </summary>
 18/// <param name="sorting">An array of sorting properties to define the comparison logic.</param>
 19/// <typeparam name="T">The type of objects to compare.</typeparam>
 20public class SortingComparer<T>(ISortingProperty<T>[] sorting) : IComparer, IComparer<T>
 21{
 2722    private readonly Func<T, object?>[] _selectors = [.. sorting.Select(x => x.ProvideExpression().Compile())];
 2723    private readonly ListSortDirection[] _directions = [.. sorting.Select(x => x.Direction)];
 24
 25    /// <summary>
 26    /// Compares two objects of type T based on the sorting properties defined in the sortCollection.
 27    /// </summary>
 28    /// <param name="x">The first object to compare.</param>
 29    /// <param name="y">The second object to compare.</param>
 30    /// <returns>A signed integer that indicates the relative values of x and y.</returns>
 31    public int Compare(T? x, T? y)
 32    {
 25833        if (ReferenceEquals(x, y)) return 0;
 25834        if (x is null) return -1;
 25835        if (y is null) return 1;
 36
 52837        for (var i = 0; i < _selectors.Length; i++)
 38        {
 26139            var selector = _selectors[i];
 40
 26141            var vx = selector(x);
 26142            var vy = selector(y);
 43
 26144            var result = Comparer<object?>.Default.Compare(vx, vy);
 45
 26146            if (result != 0)
 47            {
 25548                return _directions[i] == ListSortDirection.Ascending
 25549                    ? result
 25550                    : -result;
 51            }
 52        }
 53
 354        return 0;
 55    }
 56
 57    /// <summary>
 58    /// Compares two objects based on the sorting properties defined in the sortCollection.
 59    /// </summary>
 60    /// <param name="x">The first object to compare.</param>
 61    /// <param name="y">The second object to compare.</param>
 62    /// <returns>A signed integer that indicates the relative values of x and y.</returns>
 063    public int Compare(object? x, object? y) => Compare((T?)x, (T?)y);
 64}
 65