< Summary

Information
Class: MyNet.Collections.DefaultCollectionSorter<T>
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/DefaultCollectionSorter.cs
Tag: 323_28699572109
Line coverage
91%
Covered lines: 22
Uncovered lines: 2
Coverable lines: 24
Total lines: 93
Line coverage: 91.6%
Branch coverage
80%
Covered branches: 8
Total branches: 10
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
Sort(...)100%22100%
FindInsertIndex(...)75%8888.23%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/DefaultCollectionSorter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DefaultCollectionSorter.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.ComponentModel;
 10using System.Linq;
 11
 12namespace MyNet.Collections;
 13
 14/// <summary>
 15/// Current sorting strategy using comparer-based ordering.
 16/// </summary>
 17/// <typeparam name="T">The item type.</typeparam>
 18public sealed class DefaultCollectionSorter<T> : ICollectionSorter<T>
 19{
 20    /// <summary>
 21    /// Gets the default instance of the collection sorter that uses comparer-based ordering.
 22    /// </summary>
 923    public static DefaultCollectionSorter<T> Default { get; } = new();
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="DefaultCollectionSorter{T}"/> class.
 27    /// </summary>
 928    private DefaultCollectionSorter() { }
 29
 30    /// <summary>
 31    /// Returns a sorted snapshot of source items based on the specified selector and sort direction.
 32    /// </summary>
 33    /// <param name="source">The source collection to sort.</param>
 34    /// <param name="selector">A function to extract the key for each element.</param>
 35    /// <param name="direction">The direction to sort the elements.</param>
 36    /// <returns>A sorted list of elements.</returns>
 37    public IReadOnlyList<T> Sort(IEnumerable<T> source, Func<T, object> selector, ListSortDirection direction = ListSort
 38    {
 1839        ArgumentNullException.ThrowIfNull(source);
 1840        ArgumentNullException.ThrowIfNull(selector);
 41
 1842        return direction == ListSortDirection.Ascending
 1843            ? [.. source.OrderBy(selector)]
 1844            : source.OrderByDescending(selector).ToList();
 45    }
 46
 47    /// <summary>
 48    /// Finds the insertion index for the specified item in the sorted source collection based on the selector and sort 
 49    /// </summary>
 50    /// <param name="source">The sorted source collection.</param>
 51    /// <param name="item">The item to find the insertion index for.</param>
 52    /// <param name="selector">A function to extract the key for each element.</param>
 53    /// <param name="direction">The direction to sort the elements.</param>
 54    /// <returns>The index at which the item should be inserted.</returns>
 55    public int FindInsertIndex(IReadOnlyList<T> source, T item, Func<T, object> selector, ListSortDirection direction = 
 56    {
 657        ArgumentNullException.ThrowIfNull(source);
 658        ArgumentNullException.ThrowIfNull(selector);
 59
 660        if (source.Count == 0)
 61        {
 062            return 0;
 63        }
 64
 665        var itemKey = selector(item);
 666        var left = 0;
 667        var right = source.Count;
 68
 2169        while (left < right)
 70        {
 1571            var mid = (left + right) / 2;
 1572            var midKey = selector(source[mid]);
 1573            var comparison = Comparer<object>.Default.Compare(midKey, itemKey);
 74
 1575            if (direction == ListSortDirection.Descending)
 76            {
 077                comparison = -comparison;
 78            }
 79
 1580            if (comparison < 0)
 81            {
 682                left = mid + 1;
 83            }
 84            else
 85            {
 986                right = mid;
 87            }
 88        }
 89
 690        return left;
 91    }
 92}
 93