Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/libraries/System.Linq/src/System/Linq/OrderBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,27 @@ public static IOrderedEnumerable<TSource> ThenByDescending<TSource, TKey>(this I

/// <summary>Gets whether the results of an unstable sort will be observably the same as a stable sort.</summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool TypeIsImplicitlyStable<T>() =>
typeof(T) == typeof(sbyte) || typeof(T) == typeof(byte) ||
typeof(T) == typeof(int) || typeof(T) == typeof(uint) ||
typeof(T) == typeof(short) || typeof(T) == typeof(ushort) ||
typeof(T) == typeof(long) || typeof(T) == typeof(ulong) ||
typeof(T) == typeof(Int128) || typeof(T) == typeof(UInt128) ||
typeof(T) == typeof(nint) || typeof(T) == typeof(nuint) ||
typeof(T) == typeof(bool) || typeof(T) == typeof(char);
internal static bool TypeIsImplicitlyStable<T>()
{
Type t = typeof(T);
if (typeof(T).IsEnum)
{
t = typeof(T).GetEnumUnderlyingType();
}

// Check for integral primitive types that compare equally iff they have the same bit pattern.
// bool is included because, even though technically it can have 256 different values, anything
// other than 0/1 is only producible using unsafe code. It's tempting to include a type like string
// here, as it's so commonly used with ordering, but two different string objects can compare equally,
// and their reference identity can be observable in a stable vs unstable sort.
return
t == typeof(sbyte) || t == typeof(byte) || t == typeof(bool) ||
t == typeof(short) || t == typeof(ushort) || t == typeof(char) ||
t == typeof(int) || t == typeof(uint) ||
t == typeof(long) || t == typeof(ulong) ||
t == typeof(Int128) || t == typeof(UInt128) ||
t == typeof(nint) || t == typeof(nuint);
}
}

public interface IOrderedEnumerable<out TElement> : IEnumerable<TElement>
Expand Down