Skip to content

Game.Input.ModifiersComparer

Assembly: Assembly-CSharp
Namespace: Game.Input

Type: struct

Base: System.ValueType, System.Collections.Generic.IComparer

Summary: ModifiersComparer is a small value-type comparer used to order single-precision floating-point modifier values by their absolute magnitude in descending order. It also includes explicit handling for NaN values: NaNs are treated as "greater" than any non-NaN value so they will be ordered after regular numeric values when using typical ascending sort routines with this comparer. The struct is laid out sequentially and is optimized as a tiny, allocation-free comparer for use in sorting or ordered collections where modifier strengths (including negatives) should be ranked by magnitude.


Fields

  • (none)
    ModifiersComparer has no instance fields. It is attributed with [StructLayout(LayoutKind.Sequential, Size = 1)] to ensure a small, predictable size as a value type.

Properties

  • (none)

Constructors

  • public ModifiersComparer()
    The default parameterless constructor is the implicit default for a struct. No custom construction logic is required.

Methods

  • public int Compare(float x, float y)
    Compares two single-precision floating-point values according to the following rules:
  • If x is NaN, returns 1 (x considered greater than y).
  • Else if y is NaN, returns -1 (x considered less than y).
  • Otherwise compares absolute values:
    • If |x| > |y| returns -1 (x is ordered before y — sorts by descending absolute value).
    • If |x| < |y| returns 1.
    • If |x| == |y| returns 0.
  • Effectively this yields ordering by absolute magnitude descending, with NaNs placed after numeric values.

Usage Example

using System;
using System.Collections.Generic;
using Game.Input;

var values = new List<float> { -3.0f, 1.5f, float.NaN, -2.5f, 2.5f, 0f };

// Sort using the comparer: largest absolute values first, NaN last
values.Sort(new ModifiersComparer());

// Resulting order:
//  -3.0f  (|3.0|)
//  2.5f   (|2.5|)  or -2.5f depending on stable sort/order of equal abs
// -2.5f
//  1.5f
//  0f
//  NaN

Console.WriteLine(string.Join(", ", values));

{{ This comparer is intended for use in systems where modifier strengths (positive or negative) must be prioritized by magnitude rather than signed value, and where NaN inputs should not interfere with ordering of valid numeric modifiers. }}