This commit is contained in:
Dustin Campbell 2024-08-13 16:12:22 -07:00
Родитель 0e33edc196
Коммит 7508c52c91
4 изменённых файлов: 10 добавлений и 23 удалений

Просмотреть файл

@ -456,19 +456,14 @@ internal static class EnumerableExtensions
}
var length = newArray.Length;
using var keys = SortKey<TKey>.GetPooledArray(minimumLength: length);
if (sortHelper.ComputeKeys(newArray.AsSpan(), keySelector, keys.Span))
if (!sortHelper.ComputeKeys(newArray.AsSpan(), keySelector, keys.Span))
{
// No need to sort - keys are already ordered.
return ImmutableCollectionsMarshal.AsImmutableArray(newArray);
// The keys are not ordered, so we need to sort the array.
Array.Sort(keys.Array, newArray, 0, length, sortHelper.GetOrCreateComparer());
}
var comparer = sortHelper.GetOrCreateComparer();
Array.Sort(keys.Array, newArray, 0, length, comparer);
return ImmutableCollectionsMarshal.AsImmutableArray(newArray);
}

Просмотреть файл

@ -387,16 +387,14 @@ internal static partial class ImmutableArrayExtensions
if (sortHelper.ComputeKeys(items, keySelector, keys.Span))
{
// No need to sort - keys are already ordered.
// The keys are already ordered, so we don't need to create a new array and sort it.
return array;
}
var newArray = new TElement[length];
items.CopyTo(newArray);
var comparer = sortHelper.GetOrCreateComparer();
Array.Sort(keys.Array, newArray, 0, length, comparer);
Array.Sort(keys.Array, newArray, 0, length, sortHelper.GetOrCreateComparer());
return ImmutableCollectionsMarshal.AsImmutableArray(newArray);
}

Просмотреть файл

@ -173,17 +173,12 @@ internal static partial class ImmutableArrayExtensions
using var keys = SortKey<TKey>.GetPooledArray(minimumLength: length);
if (sortHelper.ComputeKeys(items, keySelector, keys.Span))
if (!sortHelper.ComputeKeys(items, keySelector, keys.Span))
{
// No need to sort - keys are already ordered.
return array;
// The keys are not ordered, so we need to sort the array.
Array.Sort(keys.Array, innerArray, 0, length, sortHelper.GetOrCreateComparer());
}
var comparer = sortHelper.GetOrCreateComparer();
Array.Sort(keys.Array, innerArray, 0, length, comparer);
// Because we modified the inner array, we can just return array.
return array;
}
}

Просмотреть файл

@ -1314,14 +1314,13 @@ internal static class ReadOnlyListExtensions
if (sortHelper.ComputeKeys(list, keySelector, keys.Span))
{
// No need to sort - keys are already ordered.
// The keys are already ordered, so we don't need to create a new array and sort it.
return ImmutableCollectionsMarshal.AsImmutableArray(list.ToArray());
}
var newArray = list.ToArray();
var comparer = sortHelper.GetOrCreateComparer();
Array.Sort(keys.Array, newArray, 0, length, comparer);
Array.Sort(keys.Array, newArray, 0, length, sortHelper.GetOrCreateComparer());
return ImmutableCollectionsMarshal.AsImmutableArray(newArray);
}