1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 00:53:22 +08:00

Make carousel item sorting stable

Migrate beatmap carousel item sorting from List<T>.Sort()
to IEnumerable<T>.OrderBy(), as the second variant is documented to be
a stable sorting algorithm. This allows for eliminating unnecessary
movement of carousel items occurring whenever any set of items is tied
when changing sorting criteria.
This commit is contained in:
Bartłomiej Dach 2019-10-27 23:14:14 +01:00
parent 022cc13952
commit c8d3dd0e5a

View File

@ -1,7 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
namespace osu.Game.Screens.Select.Carousel
{
@ -81,12 +83,9 @@ namespace osu.Game.Screens.Select.Carousel
{
base.Filter(criteria);
var children = new List<CarouselItem>(InternalChildren);
children.ForEach(c => c.Filter(criteria));
children.Sort((x, y) => x.CompareTo(criteria, y));
InternalChildren = children;
InternalChildren.ForEach(c => c.Filter(criteria));
// IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability
InternalChildren = InternalChildren.OrderBy(c => c, new CriteriaComparer(criteria)).ToList();
}
protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value)
@ -104,5 +103,23 @@ namespace osu.Game.Screens.Select.Carousel
State.Value = CarouselItemState.Selected;
}
}
private class CriteriaComparer : IComparer<CarouselItem>
{
private readonly FilterCriteria criteria;
public CriteriaComparer(FilterCriteria criteria)
{
this.criteria = criteria;
}
public int Compare(CarouselItem x, CarouselItem y)
{
if (x != null && y != null)
return x.CompareTo(criteria, y);
throw new ArgumentNullException();
}
}
}
}