| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SortingComparer.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; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.ComponentModel; |
| | | 11 | | using System.Linq; |
| | | 12 | | |
| | | 13 | | namespace 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> |
| | | 20 | | public class SortingComparer<T>(ISortingProperty<T>[] sorting) : IComparer, IComparer<T> |
| | | 21 | | { |
| | 27 | 22 | | private readonly Func<T, object?>[] _selectors = [.. sorting.Select(x => x.ProvideExpression().Compile())]; |
| | 27 | 23 | | 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 | | { |
| | 258 | 33 | | if (ReferenceEquals(x, y)) return 0; |
| | 258 | 34 | | if (x is null) return -1; |
| | 258 | 35 | | if (y is null) return 1; |
| | | 36 | | |
| | 528 | 37 | | for (var i = 0; i < _selectors.Length; i++) |
| | | 38 | | { |
| | 261 | 39 | | var selector = _selectors[i]; |
| | | 40 | | |
| | 261 | 41 | | var vx = selector(x); |
| | 261 | 42 | | var vy = selector(y); |
| | | 43 | | |
| | 261 | 44 | | var result = Comparer<object?>.Default.Compare(vx, vy); |
| | | 45 | | |
| | 261 | 46 | | if (result != 0) |
| | | 47 | | { |
| | 255 | 48 | | return _directions[i] == ListSortDirection.Ascending |
| | 255 | 49 | | ? result |
| | 255 | 50 | | : -result; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 3 | 54 | | 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> |
| | 0 | 63 | | public int Compare(object? x, object? y) => Compare((T?)x, (T?)y); |
| | | 64 | | } |
| | | 65 | | |