--- source_files: - DataPRO/Modules/TestSetups/TestSetupsList/Model/TestSetupComparer.cs generated_at: "2026-04-16T04:51:41.503080+00:00" model: "Qwen/Qwen3-Coder-Next-FP8" schema_version: 1 sha256: "c8e267fd60a22a5e" --- # Model ### **Purpose** The `TestSetupComparer` class provides a customizable comparison implementation for sorting collections of `ITestSetup` objects. It enables ordering test setups by various fields (e.g., name, description, timestamps) in either ascending or descending order, supporting UI-driven sorting scenarios (e.g., table column sorting). It serves as a concrete implementation of `IComparer` to integrate with .NET’s sorting infrastructure (e.g., `List.Sort`, LINQ’s `OrderBy`). --- ### **Public Interface** #### `public class TestSetupComparer : IComparer` A comparer that compares two `ITestSetup` instances based on configurable criteria. - **`public TestSetupFields SortField { get; set; } = TestSetupFields.Name;`** Gets or sets the field by which to sort. Defaults to `TestSetupFields.Name`. Must be a valid member of the `TestSetupFields` enum (defined in `DTS.Common.Enums.TestSetups.TestSetupList`). - **`public bool SortAscending { get; set; } = true;`** Gets or sets the sort direction. `true` for ascending order (default), `false` for descending. - **`public int Compare(ITestSetup left, ITestSetup right)`** Compares two `ITestSetup` instances (`left` and `right`) based on the current `SortField` and `SortAscending` settings. Returns: - `< 0` if `left` < `right` (ascending) or `left` > `right` (descending), - `0` if equal, - `> 0` if `left` > `right` (ascending) or `left` < `right` (descending). Handles `null` inputs: `null` is treated as *less than* any non-null value. If `SortField` is unrecognized, falls back to comparing hash codes. --- ### **Invariants** - `SortField` must be one of the values defined in `TestSetupFields` (from `DTS.Common.Enums.TestSetups.TestSetupList`). Invalid values are *not* validated at runtime; they fall through to the hash-code comparison fallback. - `null` is consistently ordered before non-null instances, regardless of `SortField` or `SortAscending`. - If `left == right` (reference equality), `Compare` returns `0` *before* evaluating field values. - The comparison for string fields (`Name`, `Description`, `LastModifiedBy`) is **case-insensitive** (`StringComparison.OrdinalIgnoreCase`). - The comparison for numeric/enum fields (`RecordingMode`, `PreTriggerSeconds`, `PostTriggerSeconds`, `LastModified`, `IsComplete`) uses their respective `IComparable.CompareTo` implementations. --- ### **Dependencies** - **Imports/References**: - `DTS.Common.Enums.TestSetups.TestSetupList` → Provides `TestSetupFields` enum. - `DTS.Common.Interface.TestSetups.TestSetupsList` → Defines `ITestSetup` interface (contract for test setup objects). - **Depended upon by**: - Any code requiring sorted `ITestSetup` collections (e.g., UI view models, data grids, or list processors in the `TestSetupsList` module or related layers). - *Inferred*: Likely used in conjunction with `ITestSetup` implementations (e.g., concrete test setup classes implementing the interface). --- ### **Gotchas** - **No validation of `SortField`**: If `SortField` is set to an undefined or future enum value, the `switch` falls back to `a.GetHashCode().CompareTo(b.GetHashCode())`, which yields *unstable* and *semantically meaningless* ordering. - **Hash-code fallback is unsafe for sorting**: Using `GetHashCode()` as a tiebreaker (or primary fallback) does not guarantee transitivity or consistency across runs (hash codes may vary per process), potentially violating `IComparer` contract requirements. - **No null-safety for `ITestSetup` properties**: The comparer assumes `a.Name`, `a.Description`, etc., are non-null. If any property (e.g., `LastModifiedBy`) is `null`, `string.Compare` or `CompareTo` may throw `NullReferenceException`. - **Reference equality check before null checks**: The initial `if (left == right)` check is redundant with later `if (a == b)` after swapping, but harmless. - **No handling of `DateTime` precision**: `LastModified` comparison relies on `DateTime.CompareTo`, which includes ticks—sub-millisecond differences may matter in edge cases. - **Case-insensitive string comparison may be inconsistent across cultures**: `StringComparison.OrdinalIgnoreCase` is safe for English, but not fully culture-agnostic (e.g., Turkish 'i' issues are avoided, but other edge cases may exist). *None identified beyond the above.*