| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DefaultCollectionSorter.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using System.Linq; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Collections; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Current sorting strategy using comparer-based ordering. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="T">The item type.</typeparam> |
| | | 18 | | public 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> |
| | 9 | 23 | | public static DefaultCollectionSorter<T> Default { get; } = new(); |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="DefaultCollectionSorter{T}"/> class. |
| | | 27 | | /// </summary> |
| | 9 | 28 | | 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 | | { |
| | 18 | 39 | | ArgumentNullException.ThrowIfNull(source); |
| | 18 | 40 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 41 | | |
| | 18 | 42 | | return direction == ListSortDirection.Ascending |
| | 18 | 43 | | ? [.. source.OrderBy(selector)] |
| | 18 | 44 | | : 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 | | { |
| | 6 | 57 | | ArgumentNullException.ThrowIfNull(source); |
| | 6 | 58 | | ArgumentNullException.ThrowIfNull(selector); |
| | | 59 | | |
| | 6 | 60 | | if (source.Count == 0) |
| | | 61 | | { |
| | 0 | 62 | | return 0; |
| | | 63 | | } |
| | | 64 | | |
| | 6 | 65 | | var itemKey = selector(item); |
| | 6 | 66 | | var left = 0; |
| | 6 | 67 | | var right = source.Count; |
| | | 68 | | |
| | 21 | 69 | | while (left < right) |
| | | 70 | | { |
| | 15 | 71 | | var mid = (left + right) / 2; |
| | 15 | 72 | | var midKey = selector(source[mid]); |
| | 15 | 73 | | var comparison = Comparer<object>.Default.Compare(midKey, itemKey); |
| | | 74 | | |
| | 15 | 75 | | if (direction == ListSortDirection.Descending) |
| | | 76 | | { |
| | 0 | 77 | | comparison = -comparison; |
| | | 78 | | } |
| | | 79 | | |
| | 15 | 80 | | if (comparison < 0) |
| | | 81 | | { |
| | 6 | 82 | | left = mid + 1; |
| | | 83 | | } |
| | | 84 | | else |
| | | 85 | | { |
| | 9 | 86 | | right = mid; |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | |
| | 6 | 90 | | return left; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |