CollectionsExtensions.SortedInsert<TElement> Method

Definition

Inserts an item in its sorted position into an already sorted collection. This is useful if you need to consume the collection in between insertions and need it to stay correctly sorted the whole time. If you just need to insert a bunch of items and then consume the sorted collection at the end, it's faster to add all the elements and then use Sort() at the end.

public static int SortedInsert<TElement> (this System.Collections.Generic.List<TElement> elements, TElement toInsert, System.Collections.Generic.IComparer<TElement> comparer = default);
static member SortedInsert : System.Collections.Generic.List<'Element> * 'Element * System.Collections.Generic.IComparer<'Element> -> int
<Extension()>
Public Function SortedInsert(Of TElement) (elements As List(Of TElement), toInsert As TElement, Optional comparer As IComparer(Of TElement) = Nothing) As Integer

Type Parameters

TElement

The type of element in the collection.

Parameters

elements
List<TElement>

The collection of sorted elements to be inserted into.

toInsert
TElement

The element to insert.

comparer
IComparer<TElement>

The comparer to use when sorting or null to use Default.

Returns

Applies to