Be more explicit with DatItem CompareTo

This commit is contained in:
Matt Nadareski
2025-01-07 20:21:26 -05:00
parent 49aa18bccd
commit f778735b96

View File

@@ -157,14 +157,18 @@ namespace SabreTools.DatItems
/// <inheritdoc/> /// <inheritdoc/>
public int CompareTo(DatItem? other) public int CompareTo(DatItem? other)
{ {
// If the other item doesn't exist
if (other == null)
return 1;
try try
{ {
if (GetName() == other?.GetName()) if (GetName() == other.GetName())
return Equals(other) ? 0 : 1; return Equals(other) ? 0 : (other.ItemType - ItemType);
// If `other?.GetName()` is null, Compare will return > 0 // If `other.GetName()` is null, Compare will return > 0
// If `this.GetName()` is null, Compare will return < 0 // If `this.GetName()` is null, Compare will return < 0
return string.Compare(GetName(), other?.GetName(), StringComparison.Ordinal); return string.Compare(GetName(), other.GetName(), StringComparison.Ordinal);
} }
catch catch
{ {
@@ -545,14 +549,18 @@ namespace SabreTools.DatItems
/// <inheritdoc/> /// <inheritdoc/>
public int CompareTo(DatItem<T>? other) public int CompareTo(DatItem<T>? other)
{ {
// If the other item doesn't exist
if (other == null)
return 1;
try try
{ {
if (GetName() == other?.GetName()) if (GetName() == other.GetName())
return Equals(other) ? 0 : 1; return Equals(other) ? 0 : (other.ItemType - ItemType);
// If `other?.GetName()` is null, Compare will return > 0 // If `other?.GetName()` is null, Compare will return > 0
// If `this.GetName()` is null, Compare will return < 0 // If `this.GetName()` is null, Compare will return < 0
return string.Compare(GetName(), other?.GetName(), StringComparison.Ordinal); return string.Compare(GetName(), other.GetName(), StringComparison.Ordinal);
} }
catch catch
{ {