UniversalComparer(T).Compare Method
- Last UpdatedNov 18, 2025
- 3 minute read
- PI System
- AF SDK 2024 R2
- Developer
Performs a comparison of two objects of the type and sort criteria
specified in the constructor and returns a value indicating whether
one is less than, equal to, or greater than the other.
Namespace: OSIsoft.AF
Assembly: OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.1.1.1182
Syntax
public override int Compare( T x, T y )
Public Overrides Function Compare ( x As T, y As T ) As Integer Dim instance As UniversalComparer Dim x As T Dim y As T Dim returnValue As Integer returnValue = instance.Compare(x, y)
public: virtual int Compare( T x, T y ) override
abstract Compare : x : 'T * y : 'T -> int override Compare : x : 'T * y : 'T -> int
Parameters
Return Value
Type: Int32| Value | Meaning |
|---|---|
| Less than zero | x is less than y. |
| Zero | x equals y. |
| Greater than zero | x is greater than y. |
Implements
IComparerTCompare(T, T)
Exceptions
| Exception | Condition |
|---|---|
| ArgumentException | Neither x nor y implements IComparable, IComparable{T}, is a Nullable{T} type, or x and y are of different types. |
Remarks
The objects being compared must support the IComparable
interface, the IComparable{T}
interface, or is a Nullable{T} type.
Comparing with any type is allowed and does not generate
an exception and is considered to be less than any other object.
Examples
This example will sort the AFElement items
in the array arrayElements first by type in ascending order, then
by name in descending order.
AFElements allElements = db.Elements; AFElement[] arrayElements = new AFBaseElement[allElements.Count]; allElements.CopyTo(arrayElements, 0); UniversalComparer comparer= new UniversalComparer(typeof(AFElement), "Type, Name DESC"); Array.Sort(arrayElements, comparer);