1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-27 13:23:05 +08:00

Perform ordering using IComparable instead

This commit is contained in:
Dean Herbert 2022-01-27 15:59:20 +09:00
parent f30d63107a
commit 5637fd64d6
7 changed files with 21 additions and 10 deletions

View File

@ -25,7 +25,7 @@ namespace osu.Game.Tests.NonVisual
new RulesetInfo("custom3", "Custom Ruleset 3", string.Empty, -1), new RulesetInfo("custom3", "Custom Ruleset 3", string.Empty, -1),
}; };
var orderedRulesets = rulesets.OrderBy(r => r.SortID); var orderedRulesets = rulesets.OrderBy(r => r);
// Ensure all customs are after official. // Ensure all customs are after official.
Assert.That(orderedRulesets.Select(r => r.OnlineID), Is.EqualTo(new[] { 0, 2, -1, -1, -1, -1 })); Assert.That(orderedRulesets.Select(r => r.OnlineID), Is.EqualTo(new[] { 0, 2, -1, -1, -1, -1 }));

View File

@ -42,6 +42,8 @@ namespace osu.Game.Rulesets
public bool Equals(EFRulesetInfo other) => other != null && ID == other.ID && Available == other.Available && Name == other.Name && InstantiationInfo == other.InstantiationInfo; public bool Equals(EFRulesetInfo other) => other != null && ID == other.ID && Available == other.Available && Name == other.Name && InstantiationInfo == other.InstantiationInfo;
public int CompareTo(RulesetInfo other) => ID?.CompareTo(other.ID) ?? -1;
public override bool Equals(object obj) => obj is EFRulesetInfo rulesetInfo && Equals(rulesetInfo); public override bool Equals(object obj) => obj is EFRulesetInfo rulesetInfo && Equals(rulesetInfo);
public bool Equals(IRulesetInfo other) => other is RulesetInfo b && Equals(b); public bool Equals(IRulesetInfo other) => other is RulesetInfo b && Equals(b);

View File

@ -11,7 +11,7 @@ namespace osu.Game.Rulesets
/// <summary> /// <summary>
/// A representation of a ruleset's metadata. /// A representation of a ruleset's metadata.
/// </summary> /// </summary>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo> public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>, IComparable<RulesetInfo>
{ {
/// <summary> /// <summary>
/// The user-exposed name of this ruleset. /// The user-exposed name of this ruleset.

View File

@ -24,11 +24,6 @@ namespace osu.Game.Rulesets
public string InstantiationInfo { get; set; } = string.Empty; public string InstantiationInfo { get; set; } = string.Empty;
/// <summary>
/// A best effort sort ID which provides stable ordering and puts online rulesets before non-online rulesets.
/// </summary>
public int SortID => OnlineID >= 0 ? OnlineID : Math.Abs(ShortName.GetHashCode());
public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID) public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID)
{ {
ShortName = shortName; ShortName = shortName;
@ -62,6 +57,20 @@ namespace osu.Game.Rulesets
public bool Equals(IRulesetInfo? other) => other is RulesetInfo b && Equals(b); public bool Equals(IRulesetInfo? other) => other is RulesetInfo b && Equals(b);
public int CompareTo(RulesetInfo other)
{
// Official rulesets are always given precedence for the time being.
if (OnlineID >= 0)
{
if (other.OnlineID >= 0)
return OnlineID.CompareTo(other.OnlineID);
return -1;
}
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
}
public override int GetHashCode() public override int GetHashCode()
{ {
// Importantly, ignore the underlying realm hash code, as it will usually not match. // Importantly, ignore the underlying realm hash code, as it will usually not match.

View File

@ -163,7 +163,7 @@ namespace osu.Game.Rulesets
} }
} }
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r.SortID)); availableRulesets.AddRange(detachedRulesets.OrderBy(r => r));
}); });
} }

View File

@ -89,7 +89,7 @@ namespace osu.Game.Screens.Select.Carousel
{ {
default: default:
case SortMode.Difficulty: case SortMode.Difficulty:
int ruleset = BeatmapInfo.Ruleset.SortID.CompareTo(otherBeatmap.BeatmapInfo.Ruleset.SortID); int ruleset = BeatmapInfo.Ruleset.CompareTo(otherBeatmap.BeatmapInfo.Ruleset);
if (ruleset != 0) return ruleset; if (ruleset != 0) return ruleset;
return BeatmapInfo.StarRating.CompareTo(otherBeatmap.BeatmapInfo.StarRating); return BeatmapInfo.StarRating.CompareTo(otherBeatmap.BeatmapInfo.StarRating);

View File

@ -39,7 +39,7 @@ namespace osu.Game.Screens.Select.Carousel
beatmapSet.Beatmaps beatmapSet.Beatmaps
.Where(b => !b.Hidden) .Where(b => !b.Hidden)
.OrderBy(b => b.Ruleset.SortID) .OrderBy(b => b.Ruleset)
.ThenBy(b => b.StarRating) .ThenBy(b => b.StarRating)
.Select(b => new CarouselBeatmap(b)) .Select(b => new CarouselBeatmap(b))
.ForEach(AddChild); .ForEach(AddChild);