Skip to content

Commit 7506e9d

Browse files
authored
System.Comparison<T> F# Snippets (#7537)
1 parent 9b9c937 commit 7506e9d

File tree

5 files changed

+150
-0
lines changed

5 files changed

+150
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="source.fs" />
8+
</ItemGroup>
9+
</Project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// <Snippet1>
2+
open System
3+
4+
let compareDinosByLength (x: string) (y: string) =
5+
match x with
6+
| null when isNull y ->
7+
// If x is null and y is null, they're equal.
8+
0
9+
| null ->
10+
// If x is null and y is not null, y is greater.
11+
-1
12+
| _ when isNull y->
13+
// If x is not null and y is null, x is greater.
14+
1
15+
| _ ->
16+
// If x is not null and y is not null, compare the lengths of the two strings.
17+
let retval = x.Length.CompareTo y.Length
18+
19+
if retval <> 0 then
20+
// If the strings are not of equal length, the longer string is greater.
21+
retval
22+
else
23+
// If the strings are of equal length, sort them with ordinary string comparison.
24+
x.CompareTo y
25+
26+
let display list =
27+
printfn ""
28+
for s in list do
29+
if isNull s then
30+
printfn "(null)"
31+
else
32+
printfn $"\"%s{s}\""
33+
34+
35+
let dinosaurs = ResizeArray()
36+
dinosaurs.Add "Pachycephalosaurus"
37+
dinosaurs.Add "Amargasaurus"
38+
dinosaurs.Add ""
39+
dinosaurs.Add null
40+
dinosaurs.Add "Mamenchisaurus"
41+
dinosaurs.Add "Deinonychus"
42+
display dinosaurs
43+
44+
printfn "\nSort with generic Comparison<string> delegate:"
45+
dinosaurs.Sort compareDinosByLength
46+
display dinosaurs
47+
48+
49+
// This code example produces the following output:
50+
//
51+
// "Pachycephalosaurus"
52+
// "Amargasaurus"
53+
// ""
54+
// (null)
55+
// "Mamenchisaurus"
56+
// "Deinonychus"
57+
//
58+
// Sort with generic Comparison<string> delegate:
59+
//
60+
// (null)
61+
// ""
62+
// "Deinonychus"
63+
// "Amargasaurus"
64+
// "Mamenchisaurus"
65+
// "Pachycephalosaurus"
66+
// </Snippet1>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// <Snippet1>
2+
open System
3+
4+
type CityInfo =
5+
{ City: string
6+
Country: string
7+
Population: int }
8+
9+
static member CompareByName city1 city2 =
10+
String.Compare(city1.City, city2.City)
11+
12+
static member CompareByPopulation city1 city2 =
13+
city1.Population.CompareTo city2.Population
14+
15+
static member CompareByNames city1 city2 =
16+
String.Compare(city1.Country + city1.City, city2.Country + city2.City)
17+
18+
let display cities =
19+
printfn $"""{"City",-20} {"Country",-25} {"Population",10}"""
20+
for city in cities do
21+
printfn $"{city.City,-20} {city.Country,-25} {city.Population,10:N0}"
22+
printfn ""
23+
24+
let NYC = { City = "New York City"; Country = "United States of America"; Population = 8175133 }
25+
let Det = { City = "Detroit"; Country = "United States of America"; Population = 713777 }
26+
let Paris = { City = "Paris"; Country = "France"; Population = 2193031 }
27+
let cities = [| NYC; Det; Paris |]
28+
// Display ordered array.
29+
display cities
30+
31+
// Sort array by city name.
32+
Array.Sort(cities, CityInfo.CompareByName)
33+
display cities
34+
35+
// Sort array by population.
36+
Array.Sort(cities, CityInfo.CompareByPopulation);
37+
display cities
38+
39+
// Sort array by country + city name.
40+
Array.Sort(cities, CityInfo.CompareByNames);
41+
display cities
42+
43+
44+
// The example displays the following output:
45+
// City Country Population
46+
// New York City United States of America 8,175,133
47+
// Detroit United States of America 713,777
48+
// Paris France 2,193,031
49+
//
50+
// City Country Population
51+
// Detroit United States of America 713,777
52+
// New York City United States of America 8,175,133
53+
// Paris France 2,193,031
54+
//
55+
// City Country Population
56+
// Detroit United States of America 713,777
57+
// Paris France 2,193,031
58+
// New York City United States of America 8,175,133
59+
//
60+
// City Country Population
61+
// Paris France 2,193,031
62+
// Detroit United States of America 713,777
63+
// New York City United States of America 8,175,133
64+
// </Snippet1>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net6.0</TargetFramework>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="comparisont1.fs" />
8+
</ItemGroup>
9+
</Project>

xml/System/Comparison`1.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@
9494
9595
:::code language="cpp" source="~/samples/snippets/cpp/VS_Snippets_CLR/List`1_SortComparison/cpp/source.cpp" id="Snippet1":::
9696
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR/List`1_SortComparison/cs/source.cs" interactive="try-dotnet" id="Snippet1":::
97+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR/List`1_SortComparison/fs/source.fs" id="Snippet1":::
9798
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR/List`1_SortComparison/vb/source.vb" id="Snippet1":::
9899
99100
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.
100101
101102
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.comparison`1/cs/comparisont1.cs" interactive="try-dotnet" id="Snippet1":::
103+
:::code language="fsharp" source="~/samples/snippets/fsharp/VS_Snippets_CLR_System/system.comparison`1/fs/comparisont1.fs" id="Snippet1":::
102104
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.comparison`1/vb/comparisont1.vb" id="Snippet1":::
103105
104106
]]></format>

0 commit comments

Comments
 (0)