Click or drag to resize
Accord.NET (logo)

SortNthElementT Method (T, Int32, Int32, Int32, FuncT, T, Int32, Boolean)

Retrieves the n-th smallest element in an array. See remarks for more info.

Namespace:  Accord
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static T NthElement<T>(
	this T[] items,
	int first,
	int last,
	int n,
	Func<T, T, int> compare,
	bool asc = true
)
Request Example View Source

Parameters

items
Type: T
The array of elements from which the n-th element should be extracted.
first
Type: SystemInt32
The beginning of the search interval.
last
Type: SystemInt32
The end of the search interval.
n
Type: SystemInt32
The position to look for (0 returns the smallest element, 1 the second smallest, and so on).
compare
Type: SystemFuncT, T, Int32
The comparison function to be used to sort elements.
asc (Optional)
Type: SystemBoolean
Whether to take the smallest or the largest element. If set to false, instead of returning the smallest, the method will return the largest elements.

Type Parameters

T
The type for the items in the array.

Return Value

Type: T
If asc is true, returns the n-th smallest element in the array. Otherwise, returns the n-th largest.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks
As a side-effect, partially orders the collection, making sure every element smaller than the n-th smaller element are in the beginning of the array (but not necessarily in order). If you need the values that come before the n-th element to also be in order, please use the PartialT(T, Int32, Boolean) method instead.
See Also