Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

System.Collections.Generic.IComparer: practical reference

System.Collections.Generic.IComparer: practical reference: IComparer<T> defines ordering by returning a negative value, zero, or a positive value from Compare.

Version: .NET API; verified on .NET 10.0.14

Syntax

System.Collections.Generic.IComparer /* select a documented member or constructor */

IComparer defines ordering by returning a negative value, zero, or a positive value from Compare.

Use it as an explicit sorting policy and as the ordering rule for sorted collections.

The comparison must be transitive and consistent; callers must not expect the exact values minus one or one.

Compare(x,x) should return zero, and the ordering of null values must be chosen deliberately.

The complete program actually uses System.Collections.Generic.IComparer; its exact expected stdout is True.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

IComparer<string> comparer = StringComparer.OrdinalIgnoreCase;
Console.WriteLine(comparer.Compare("a", "B") < 0);

Result

True

The complete program actually uses `System.Collections.Generic.IComparer`; its exact expected stdout is `True`.

Sources