From bd70fbc4168a432c93fbf849810e88d4bb5a4558 Mon Sep 17 00:00:00 2001 From: Lars Date: Sat, 19 Aug 2023 02:27:42 +0200 Subject: [PATCH] `Entity` now implements `IComparable`. --- src/Arch/Core/Entity.cs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Arch/Core/Entity.cs b/src/Arch/Core/Entity.cs index 8b9843af..f89cd21b 100644 --- a/src/Arch/Core/Entity.cs +++ b/src/Arch/Core/Entity.cs @@ -13,7 +13,7 @@ namespace Arch.Core; /// represents a general-purpose object and can be assigned a set of components that act as data. /// [SkipLocalsInit] -public readonly struct Entity : IEquatable +public readonly struct Entity : IEquatable, IComparable { /// /// Its Id, unique in its . @@ -58,6 +58,18 @@ public override bool Equals(object? obj) return obj is Entity other && Equals(other); } + /// + /// Compares this instace to another one for sorting and ordering. + /// Orders them by id. Ascending. + /// + /// The other . + /// A int indicating their order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int CompareTo(Entity other) + { + return Id.CompareTo(other.Id); + } + /// /// Calculates the hash of this . /// @@ -115,7 +127,7 @@ public override string ToString() /// [DebuggerTypeProxy(typeof(EntityDebugView))] [SkipLocalsInit] -public readonly struct Entity : IEquatable +public readonly struct Entity : IEquatable, IComparable { /// /// Its Id, unique in its . @@ -175,6 +187,19 @@ public override bool Equals(object obj) return obj is Entity other && Equals(other); } + + /// + /// Compares this instace to another one for sorting and ordering. + /// Orders them by id and world. Ascending. + /// + /// The other . + /// A int indicating their order. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int CompareTo(Entity other) + { + return WorldId != other.WorldId ? WorldId.CompareTo(other.WorldId) : Id.CompareTo(other.Id); + } + /// /// Calculates the hash of this . ///