Header menu logo FSharp.Core

ComparisonIdentity Module

Common notions of value ordering implementing the IComparer interface, for constructing sorted data structures and performing sorting operations.

Functions and values

Function or value Description

FromFunction comparer

Full Usage: FromFunction comparer

Parameters:
    comparer : 'T -> 'T -> int - A function to compare two values.

Returns: IComparer<'T> An object implementing IComparer using the supplied function.

Get an implementation of comparison semantics using the given function.

comparer : 'T -> 'T -> int

A function to compare two values.

Returns: IComparer<'T>

An object implementing IComparer using the supplied function.

Example

Create and use a comparer using the given function:

 let comparer = ComparisonIdentity.FromFunction(fun i1 i2 -> compare (i1%5) (i2%5))

 comparer.Compare(7, 2)
val comparer: System.Collections.Generic.IComparer<int>
module ComparisonIdentity from Microsoft.FSharp.Collections
val FromFunction: comparer: ('T -> 'T -> int) -> System.Collections.Generic.IComparer<'T>
val i1: int
val i2: int
val compare: e1: 'T -> e2: 'T -> int (requires comparison)
System.Collections.Generic.IComparer.Compare(x: int, y: int) : int
Evaluates to 0because 7 and 2 compare as equal using to the provided function.

NonStructural

Full Usage: NonStructural

Returns: IComparer<^T> An object implementing IComparer using NonStructuralComparison.Compare.
Modifiers: inline
Type parameters: ^T

Get an implementation of comparison semantics using non-structural comparison.

Returns: IComparer<^T>

An object implementing IComparer using NonStructuralComparison.Compare.

Example

Create and use a comparer using structural comparison:

 let comparer = ComparisonIdentity.NonStructural<System.DateTime>

 comparer.Compare(System.DateTime.Now, System.DateTime.Today)
val comparer: System.Collections.Generic.IComparer<System.DateTime>
module ComparisonIdentity from Microsoft.FSharp.Collections
val NonStructural<'T (requires member (<) and member (>))> : System.Collections.Generic.IComparer<'T> (requires member (<) and member (>))
namespace System
Multiple items
[<Struct>] type DateTime = new: date: DateOnly * time: TimeOnly -> unit + 16 overloads member Add: value: TimeSpan -> DateTime member AddDays: value: float -> DateTime member AddHours: value: float -> DateTime member AddMicroseconds: value: float -> DateTime member AddMilliseconds: value: float -> DateTime member AddMinutes: value: float -> DateTime member AddMonths: months: int -> DateTime member AddSeconds: value: float -> DateTime member AddTicks: value: int64 -> DateTime ...
<summary>Represents an instant in time, typically expressed as a date and time of day.</summary>

--------------------
System.DateTime ()
   (+0 other overloads)
System.DateTime(ticks: int64) : System.DateTime
   (+0 other overloads)
System.DateTime(date: System.DateOnly, time: System.TimeOnly) : System.DateTime
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(date: System.DateOnly, time: System.TimeOnly, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : System.DateTime
   (+0 other overloads)
System.Collections.Generic.IComparer.Compare(x: System.DateTime, y: System.DateTime) : int
property System.DateTime.Now: System.DateTime with get
<summary>Gets a <see cref="T:System.DateTime" /> object that is set to the current date and time on this computer, expressed as the local time.</summary>
<returns>An object whose value is the current local date and time.</returns>
property System.DateTime.Today: System.DateTime with get
<summary>Gets the current date.</summary>
<returns>An object that is set to today's date, with the time component set to 00:00:00.</returns>
Evaluates to 1.

Structural

Full Usage: Structural

Returns: IComparer<'T> An object implementing IComparer using Operators.compare.
Modifiers: inline
Type parameters: 'T

Get an implementation of comparison semantics using structural comparison.

Returns: IComparer<'T>

An object implementing IComparer using Operators.compare.

Example

Create and use a comparer using structural comparison:

 let compareTuples = ComparisonIdentity.Structural<int * int>

 compareTuples.Compare((1, 4), (1, 5))
val compareTuples: System.Collections.Generic.IComparer<int * int>
module ComparisonIdentity from Microsoft.FSharp.Collections
val Structural<'T (requires comparison)> : System.Collections.Generic.IComparer<'T> (requires comparison)
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
System.Collections.Generic.IComparer.Compare(x: int * int, y: int * int) : int
Evaluates to -1.

Type something to start searching.