diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs index a5b3a5b32..af1819d77 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterGenerator.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using static PKHeX.Core.Legal; @@ -420,5 +421,32 @@ internal static bool IsEncounterTrade1Valid(PKM pkm, EncounterTrade t) var tr = t.GetOT(pkm.Language); return ot == tr; } + + /// + /// Filters a sequence of values based on a predicate. + /// Any elements that match the predicate are yielded after those that did not match, in the same order they were observed. + /// + /// + /// OrderBy consumes the entire list when reordering elements, instead of instantly yielding best matches. + /// https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,ffb8de6aefac77cc + /// The type of the elements of source. + /// A sequence of values to reorder. + /// A function to test each element for a condition. + /// An that contains elements from the input, with non-deferred results first. + internal static IEnumerable DeferByBoolean(this IEnumerable source, Func predicate) + { + var deferred = new List(); + foreach (var x in source) + { + if (predicate(x)) + { + deferred.Add(x); + continue; + } + yield return x; + } + foreach (var d in deferred) + yield return d; + } } } diff --git a/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs b/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs index 5e1bd2eee..ef90e4fde 100644 --- a/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs +++ b/PKHeX.Core/Legality/Encounters/Generator/EncounterSlotGenerator.cs @@ -64,7 +64,7 @@ public static IEnumerable GetValidWildEncounters(PKM pkm, GameVer bool IsHidden = pkm.AbilityNumber == 4; // hidden Ability int species = pkm.Species; - return s.OrderBy(slot => slot.IsDeferred(species, pkm, IsSafariBall, IsSportBall, IsHidden)); // non-deferred first + return s.DeferByBoolean(slot => slot.IsDeferred(species, pkm, IsSafariBall, IsSportBall, IsHidden)); // non-deferred first } public static bool IsDeferred3(this EncounterSlot slot, int currentSpecies, PKM pkm, bool IsSafariBall) {