Class CustomComparer<T>
Custom comparer which accepts any delegate or
anonymous function to perform value comparisons.
Inheritance
System.Object
CustomComparer<T>
Implements
System.Collections.Generic.IComparer<T>
System.Collections.Generic.IEqualityComparer<T>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Mars.Numerics.Comparers
Assembly: Mars.Numerics.dll
Syntax
public class CustomComparer<T> : IComparer<T>, IEqualityComparer<T>
Type Parameters
Name | Description |
---|---|
T | The type of objects to compare. |
Examples
// Assume we have values to sort
double[] values = { 0, 5, 3, 1, 8 };
// We can create an ad-hoc sorting rule using
Array.Sort(values, new CustomComparer<double>((a, b) => -a.CompareTo(b)));
// Result will be { 8, 5, 3, 1, 0 }.
Constructors
| Improve this Doc View SourceCustomComparer(Func<T, T, Int32>)
Constructs a new CustomComparer<T>.
Declaration
public CustomComparer(Func<T, T, int> comparer)
Parameters
Type | Name | Description |
---|---|---|
System.Func<T, T, System.Int32> | comparer | The comparer function. |
Methods
| Improve this Doc View SourceCompare(T, T)
Compares two objects and returns a value indicating
whether one is less than, equal to, or greater than
the other.
Declaration
public int Compare(T x, T y)
Parameters
Type | Name | Description |
---|---|---|
T | x | The first object to compare. |
T | y | The second object to compare. |
Returns
Type | Description |
---|---|
System.Int32 | A signed integer that indicates the relative values of x and y. |
Equals(T, T)
Determines whether the specified objects are equal.
Declaration
public bool Equals(T x, T y)
Parameters
Type | Name | Description |
---|---|---|
T | x | The first object of type T to compare. |
T | y | The second object of type T to compare. |
Returns
Type | Description |
---|---|
System.Boolean | true if the specified objects are equal; otherwise, false. |
GetHashCode(T)
Returns a hash code for the given object.
Declaration
public int GetHashCode(T obj)
Parameters
Type | Name | Description |
---|---|---|
T | obj | The object. |
Returns
Type | Description |
---|---|
System.Int32 | A hash code for the given object, suitable for use in hashing algorithms and data structures like a hash table. |
Implements
System.Collections.Generic.IComparer<T>
System.Collections.Generic.IEqualityComparer<T>