mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-12 18:27:23 +01:00
29 lines
1 KiB
C#
29 lines
1 KiB
C#
using System.Runtime.CompilerServices;
|
|
|
|
namespace Dalamud.Interface;
|
|
|
|
internal static class StableInsertionSortExtension
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
public static void StableSort<T, TKey>(this IList<T> list, Func<T, TKey> selector)
|
|
{
|
|
var tmpList = new List<T>(list.Count);
|
|
tmpList.AddRange(list.OrderBy(selector));
|
|
for (var i = 0; i < tmpList.Count; ++i)
|
|
list[i] = tmpList[i];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
|
|
public static void StableSort<T>(this IList<T> list, Comparison<T> comparer)
|
|
{
|
|
var tmpList = new List<(T, int)>(list.Count);
|
|
tmpList.AddRange(list.WithIndex());
|
|
tmpList.Sort((a, b) =>
|
|
{
|
|
var ret = comparer(a.Item1, b.Item1);
|
|
return ret != 0 ? ret : a.Item2.CompareTo(b.Item2);
|
|
});
|
|
for (var i = 0; i < tmpList.Count; ++i)
|
|
list[i] = tmpList[i].Item1;
|
|
}
|
|
}
|