Skip to content

Commit

Permalink
Entity now implements IComparable<Entity>.
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Aug 19, 2023
1 parent 15623cc commit 9ff7f80
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Arch/Core/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Arch.Core;
/// represents a general-purpose object and can be assigned a set of components that act as data.
/// </summary>
[SkipLocalsInit]
public readonly struct Entity : IEquatable<Entity>
public readonly struct Entity : IEquatable<Entity>, IComparable<Entity>
{
/// <summary>
/// Its Id, unique in its <see cref="World"/>.
Expand Down Expand Up @@ -58,6 +58,18 @@ public override bool Equals(object? obj)
return obj is Entity other && Equals(other);
}

/// <summary>
/// Compares this <see cref="Entity"/> instace to another one for sorting and ordering.
/// <remarks>Orders them by id. Ascending.</remarks>
/// </summary>
/// <param name="other">The other <see cref="Entity"/>.</param>
/// <returns>A int indicating their order.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(Entity other)
{
return Id.CompareTo(other.Id);
}

/// <summary>
/// Calculates the hash of this <see cref="Entity"/>.
/// </summary>
Expand Down Expand Up @@ -115,7 +127,7 @@ public override string ToString()
/// </summary>
[DebuggerTypeProxy(typeof(EntityDebugView))]
[SkipLocalsInit]
public readonly struct Entity : IEquatable<Entity>
public readonly struct Entity : IEquatable<Entity>, IComparable<Entity>
{
/// <summary>
/// Its Id, unique in its <see cref="World"/>.
Expand Down Expand Up @@ -175,6 +187,19 @@ public override bool Equals(object obj)
return obj is Entity other && Equals(other);
}


/// <summary>
/// Compares this <see cref="Entity"/> instace to another one for sorting and ordering.
/// <remarks>Orders them by id and world. Ascending.</remarks>
/// </summary>
/// <param name="other">The other <see cref="Entity"/>.</param>
/// <returns>A int indicating their order.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(Entity other)
{
return WorldId != other.WorldId ? WorldId.CompareTo(other.WorldId) : Id.CompareTo(other.Id);
}

/// <summary>
/// Calculates the hash of this <see cref="Entity"/>.
/// </summary>
Expand Down

0 comments on commit 9ff7f80

Please sign in to comment.