Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.Comparison<T> F# Snippets #7537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="source.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// <Snippet1>
open System

let compareDinosByLength (x: string) (y: string) =
match x with
| null when isNull y ->
// If x is null and y is null, they're equal.
0
| null ->
// If x is null and y is not null, y is greater.
-1
| _ when isNull y->
// If x is not null and y is null, x is greater.
1
| _ ->
// If x is not null and y is not null, compare the lengths of the two strings.
let retval = x.Length.CompareTo y.Length

if retval <> 0 then
// If the strings are not of equal length, the longer string is greater.
retval
else
// If the strings are of equal length, sort them with ordinary string comparison.
x.CompareTo y

let display list =
printfn ""
for s in list do
if isNull s then
printfn "(null)"
else
printfn $"\"%s{s}\""


let dinosaurs = ResizeArray()
dinosaurs.Add "Pachycephalosaurus"
dinosaurs.Add "Amargasaurus"
dinosaurs.Add ""
dinosaurs.Add null
dinosaurs.Add "Mamenchisaurus"
dinosaurs.Add "Deinonychus"
display dinosaurs

printfn "\nSort with generic Comparison<string> delegate:"
dinosaurs.Sort compareDinosByLength
display dinosaurs


// This code example produces the following output:
//
// "Pachycephalosaurus"
// "Amargasaurus"
// ""
// (null)
// "Mamenchisaurus"
// "Deinonychus"
//
// Sort with generic Comparison<string> delegate:
//
// (null)
// ""
// "Deinonychus"
// "Amargasaurus"
// "Mamenchisaurus"
// "Pachycephalosaurus"
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// <Snippet1>
open System

type CityInfo =
{ City: string
Country: string
Population: int }

static member CompareByName city1 city2 =
String.Compare(city1.City, city2.City)

static member CompareByPopulation city1 city2 =
city1.Population.CompareTo city2.Population

static member CompareByNames city1 city2 =
String.Compare(city1.Country + city1.City, city2.Country + city2.City)

let display cities =
printfn $"""{"City",-20} {"Country",-25} {"Population",10}"""
for city in cities do
printfn $"{city.City,-20} {city.Country,-25} {city.Population,10:N0}"
printfn ""

let NYC = { City = "New York City"; Country = "United States of America"; Population = 8175133 }
let Det = { City = "Detroit"; Country = "United States of America"; Population = 713777 }
let Paris = { City = "Paris"; Country = "France"; Population = 2193031 }
let cities = [| NYC; Det; Paris |]
// Display ordered array.
display cities

// Sort array by city name.
Array.Sort(cities, CityInfo.CompareByName)
display cities

// Sort array by population.
Array.Sort(cities, CityInfo.CompareByPopulation);
display cities

// Sort array by country + city name.
Array.Sort(cities, CityInfo.CompareByNames);
display cities


// The example displays the following output:
// City Country Population
// New York City United States of America 8,175,133
// Detroit United States of America 713,777
// Paris France 2,193,031
//
// City Country Population
// Detroit United States of America 713,777
// New York City United States of America 8,175,133
// Paris France 2,193,031
//
// City Country Population
// Detroit United States of America 713,777
// Paris France 2,193,031
// New York City United States of America 8,175,133
//
// City Country Population
// Paris France 2,193,031
// Detroit United States of America 713,777
// New York City United States of America 8,175,133
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="comparisont1.fs" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions xml/System/Comparison`1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@

:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/List`1_SortComparison/cpp/source.cpp" id="Snippet1":::
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/List`1_SortComparison/cs/source.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/List`1_SortComparison/vb/source.vb" id="Snippet1":::

The following example uses the <xref:System.Comparison%601> delegate to sort the elements of a collection of `CityInfo` objects. `CityInfo` is an application-defined class that contains information about a city and its population. The example defines three methods, `CompareByName`, `CompareByPopulation`, and `CompareByNames`, that offer three different ways of ordering the `CityInfo` objects. Each method is assigned to the `comparison` argument of the <xref:System.Array.Sort%60%601%28%60%600%5B%5D%2CSystem.Comparison%7B%60%600%7D%29?displayProperty=nameWithType> method.

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.comparison`1/cs/comparisont1.cs" interactive="try-dotnet" id="Snippet1":::
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs" id="Snippet1":::
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.comparison`1/vb/comparisont1.vb" id="Snippet1":::

]]></format>
Expand Down