Sdílet prostřednictvím


Comparer<T> Třída

Definice

Poskytuje základní třídu pro implementace IComparer<T> obecného rozhraní.

generic <typename T>
public ref class Comparer abstract : System::Collections::Generic::IComparer<T>, System::Collections::IComparer
public abstract class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer
[System.Serializable]
public abstract class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer
type Comparer<'T> = class
    interface IComparer<'T>
    interface IComparer
[<System.Serializable>]
type Comparer<'T> = class
    interface IComparer
    interface IComparer<'T>
Public MustInherit Class Comparer(Of T)
Implements IComparer, IComparer(Of T)

Parametry typu

T

Typ objektů, které chcete porovnat.

Dědičnost
Comparer<T>
Atributy
Implementuje

Příklady

Následující příklad odvozuje třídu , BoxLengthFirstz Comparer<T> třídy. Tento porovnávač porovnává dva objekty typu Box. Seřadí je nejdřív podle délky, pak podle výšky a potom podle šířky. Třída Box implementuje IComparable<T> rozhraní pro řízení výchozí porovnání mezi dvěma Box objekty. Tato výchozí implementace seřadí nejprve podle výšky, pak podle délky a potom podle šířky. Příklad ukazuje rozdíly mezi těmito dvěma porovnáními tím, že nejprve seřadíte seznam Box objektů pomocí BoxLengthFirst porovnávače a pak použijete výchozí porovnávací nástroj.

using System;
using System.Collections;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        List<Box> Boxes = new List<Box>();
        Boxes.Add(new Box(4, 20, 14));
        Boxes.Add(new Box(12, 12, 12));
        Boxes.Add(new Box(8, 20, 10));
        Boxes.Add(new Box(6, 10, 2));
        Boxes.Add(new Box(2, 8, 4));
        Boxes.Add(new Box(2, 6, 8));
        Boxes.Add(new Box(4, 12, 20));
        Boxes.Add(new Box(18, 10, 4));
        Boxes.Add(new Box(24, 4, 18));
        Boxes.Add(new Box(10, 4, 16));
        Boxes.Add(new Box(10, 2, 10));
        Boxes.Add(new Box(6, 18, 2));
        Boxes.Add(new Box(8, 12, 4));
        Boxes.Add(new Box(12, 10, 8));
        Boxes.Add(new Box(14, 6, 6));
        Boxes.Add(new Box(16, 6, 16));
        Boxes.Add(new Box(2, 8, 12));
        Boxes.Add(new Box(4, 24, 8));
        Boxes.Add(new Box(8, 6, 20));
        Boxes.Add(new Box(18, 18, 12));

        // Sort by an Comparer<T> implementation that sorts
        // first by the length.
        Boxes.Sort(new BoxLengthFirst());

        Console.WriteLine("H - L - W");
        Console.WriteLine("==========");
        foreach (Box bx in Boxes)
        {
            Console.WriteLine("{0}\t{1}\t{2}",
                bx.Height.ToString(), bx.Length.ToString(),
                bx.Width.ToString());
        }

        Console.WriteLine();
        Console.WriteLine("H - L - W");
        Console.WriteLine("==========");

        // Get the default comparer that
        // sorts first by the height.
        Comparer<Box> defComp = Comparer<Box>.Default;

        // Calling Boxes.Sort() with no parameter
        // is the same as calling Boxs.Sort(defComp)
        // because they are both using the default comparer.
        Boxes.Sort();

        foreach (Box bx in Boxes)
        {
            Console.WriteLine("{0}\t{1}\t{2}",
                bx.Height.ToString(), bx.Length.ToString(),
                bx.Width.ToString());
        }


        // This explicit interface implementation
        // compares first by the length.
        // Returns -1 because the length of BoxA
        // is less than the length of BoxB.
        BoxLengthFirst LengthFirst = new BoxLengthFirst();

        Comparer<Box> bc = (Comparer<Box>) LengthFirst;

        Box BoxA = new Box(2, 6, 8);
        Box BoxB = new Box(10, 12, 14);
        int x = LengthFirst.Compare(BoxA, BoxB);
        Console.WriteLine();
        Console.WriteLine(x.ToString());
    }
}

public class BoxLengthFirst : Comparer<Box>
{
    // Compares by Length, Height, and Width.
    public override int Compare(Box x, Box y)
    {
        if (x.Length.CompareTo(y.Length) != 0)
        {
            return x.Length.CompareTo(y.Length);
        }
        else if (x.Height.CompareTo(y.Height) != 0)
        {
            return x.Height.CompareTo(y.Height);
        }
        else if (x.Width.CompareTo(y.Width) != 0)
        {
            return x.Width.CompareTo(y.Width);
        }
        else
        {
            return 0;
        }
    }
}

// This class is not demonstrated in the Main method
// and is provided only to show how to implement
// the interface. It is recommended to derive
// from Comparer<T> instead of implementing IComparer<T>.
public class BoxComp : IComparer<Box>
{
    // Compares by Height, Length, and Width.
    public int Compare(Box x, Box y)
    {
        if (x.Height.CompareTo(y.Height) != 0)
        {
            return x.Height.CompareTo(y.Height);
        }
        else if (x.Length.CompareTo(y.Length) != 0)
        {
            return x.Length.CompareTo(y.Length);
        }
        else if (x.Width.CompareTo(y.Width) != 0)
        {
            return x.Width.CompareTo(y.Width);
        }
        else
        {
            return 0;
        }
    }
}

public class Box : IComparable<Box>
{

    public Box(int h, int l, int w)
    {
        this.Height = h;
        this.Length = l;
        this.Width = w;
    }
    public int Height { get; private set; }
    public int Length { get; private set; }
    public int Width { get; private set; }

    public int CompareTo(Box other)
    {
        // Compares Height, Length, and Width.
        if (this.Height.CompareTo(other.Height) != 0)
        {
            return this.Height.CompareTo(other.Height);
        }
        else if (this.Length.CompareTo(other.Length) != 0)
        {
            return this.Length.CompareTo(other.Length);
        }
        else if (this.Width.CompareTo(other.Width) != 0)
        {
            return this.Width.CompareTo(other.Width);
        }
        else
        {
            return 0;
        }
    }
}
open System
open System.Collections.Generic

type Box(h, l, w) =
    member val Height: int = h with get
    member val Length: int = l with get
    member val Width: int = w with get

    interface IComparable<Box> with
        member this.CompareTo(other) =
            // Compares Height, Length, and Width.
            if this.Height.CompareTo other.Height <> 0 then
                this.Height.CompareTo other.Height
            elif this.Length.CompareTo other.Length <> 0 then
                this.Length.CompareTo other.Length
            elif this.Width.CompareTo other.Width <> 0 then
                this.Width.CompareTo other.Width
            else
                0

type BoxLengthFirst() =
    inherit Comparer<Box>()

    // Compares by Length, Height, and Width.
    override _.Compare(x: Box, y: Box) =
        if x.Length.CompareTo y.Length <> 0 then
            x.Length.CompareTo y.Length
        elif x.Height.CompareTo y.Height <> 0 then
            x.Height.CompareTo y.Height
        elif x.Width.CompareTo y.Width <> 0 then
            x.Width.CompareTo y.Width
        else
            0

let boxes = ResizeArray<Box>()
boxes.Add(Box(4, 20, 14))
boxes.Add(Box(12, 12, 12))
boxes.Add(Box(8, 20, 10))
boxes.Add(Box(6, 10, 2))
boxes.Add(Box(2, 8, 4))
boxes.Add(Box(2, 6, 8))
boxes.Add(Box(4, 12, 20))
boxes.Add(Box(18, 10, 4))
boxes.Add(Box(24, 4, 18))
boxes.Add(Box(10, 4, 16))
boxes.Add(Box(10, 2, 10))
boxes.Add(Box(6, 18, 2))
boxes.Add(Box(8, 12, 4))
boxes.Add(Box(12, 10, 8))
boxes.Add(Box(14, 6, 6))
boxes.Add(Box(16, 6, 16))
boxes.Add(Box(2, 8, 12))
boxes.Add(Box(4, 24, 8))
boxes.Add(Box(8, 6, 20))
boxes.Add(Box(18, 18, 12))

// Sort by an Comparer<T> implementation that sorts
// first by the length.
boxes.Sort(BoxLengthFirst())

printfn "H - L - W"
printfn "=========="

for bx in boxes do
    printfn $"{bx.Height}\t{bx.Length}\t{bx.Width}"

printfn ""
printfn "H - L - W"
printfn "=========="

// Get the default comparer that
// sorts first by the height.
let defComp = Comparer<Box>.Default

// Calling Boxes.Sort() with no parameter
// is the same as calling boxes.Sort defComp
// because they are both using the default comparer.
boxes.Sort()

for bx in boxes do
    printfn $"{bx.Height}\t{bx.Length}\t{bx.Width}"


// This explicit interface implementation
// compares first by the length.
// Returns -1 because the length of BoxA
// is less than the length of BoxB.
let LengthFirst = BoxLengthFirst()

let bc = LengthFirst :> Comparer<Box>

let BoxA = Box(2, 6, 8)
let BoxB = Box(10, 12, 14)
let x = LengthFirst.Compare(BoxA, BoxB)
printfn $"\n{x}"

// This class is not demonstrated in the Main method
// and is provided only to show how to implement
// the interface. It is recommended to derive
// from Comparer<T> instead of implementing IComparer<T>.
type BoxComp() =
    interface IComparer<Box> with
        // Compares by Height, Length, and Width.
        member _.Compare(x: Box, y: Box) =
            if x.Height.CompareTo(y.Height) <> 0 then
                x.Height.CompareTo(y.Height)
            elif x.Length.CompareTo(y.Length) <> 0 then
                x.Length.CompareTo(y.Length)
            elif x.Width.CompareTo(y.Width) <> 0 then
                x.Width.CompareTo(y.Width)
            else
                0
Imports System.Collections.Generic

Friend Class Program
    Shared Sub Main(ByVal args() As String)
        Dim Boxes As New List(Of Box)()
        Boxes.Add(New Box(4, 20, 14))
        Boxes.Add(New Box(12, 12, 12))
        Boxes.Add(New Box(8, 20, 10))
        Boxes.Add(New Box(6, 10, 2))
        Boxes.Add(New Box(2, 8, 4))
        Boxes.Add(New Box(2, 6, 8))
        Boxes.Add(New Box(4, 12, 20))
        Boxes.Add(New Box(18, 10, 4))
        Boxes.Add(New Box(24, 4, 18))
        Boxes.Add(New Box(10, 4, 16))
        Boxes.Add(New Box(10, 2, 10))
        Boxes.Add(New Box(6, 18, 2))
        Boxes.Add(New Box(8, 12, 4))
        Boxes.Add(New Box(12, 10, 8))
        Boxes.Add(New Box(14, 6, 6))
        Boxes.Add(New Box(16, 6, 16))
        Boxes.Add(New Box(2, 8, 12))
        Boxes.Add(New Box(4, 24, 8))
        Boxes.Add(New Box(8, 6, 20))
        Boxes.Add(New Box(18, 18, 12))

        ' Sort by an Comparer<T> implementation that sorts
        ' first by the length.
        Boxes.Sort(New BoxLengthFirst())

        Console.WriteLine("H - L - W")
        Console.WriteLine("==========")
        For Each bx As Box In Boxes
            Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", _
                              bx.Height.ToString(), bx.Length.ToString(), _
                              bx.Width.ToString())
        Next bx

        Console.WriteLine()
        Console.WriteLine("H - L - W")
        Console.WriteLine("==========")

        ' Get the default comparer that 
        ' sorts first by the height.
        Dim defComp As Comparer(Of Box) = Comparer(Of Box).Default

        ' Calling Boxes.Sort() with no parameter
        ' is the same as calling Boxs.Sort(defComp)
        ' because they are both using the default comparer.
        Boxes.Sort()

        For Each bx As Box In Boxes
            Console.WriteLine("{0}" & vbTab & "{1}" & vbTab & "{2}", _
                              bx.Height.ToString(), _
                              bx.Length.ToString(), _
                              bx.Width.ToString())
        Next bx


        ' This explicit interface implementation
        ' compares first by the length.
        ' Returns -1 because the length of BoxA
        ' is less than the length of BoxB.
        Dim LengthFirst As New BoxLengthFirst()

        Dim bc As Comparer(Of Box) = CType(LengthFirst, Comparer(Of Box))

        Dim BoxA As New Box(2, 6, 8)
        Dim BoxB As New Box(10, 12, 14)
        Dim x As Integer = LengthFirst.Compare(BoxA, BoxB)
        Console.WriteLine()
        Console.WriteLine(x.ToString())



    End Sub

End Class

Public Class BoxLengthFirst
    Inherits Comparer(Of Box)
    ' Compares by Length, Height, and Width.
    Public Overrides Function Compare(ByVal x As Box, ByVal y As Box) As Integer
        If x.Length.CompareTo(y.Length) <> 0 Then
            Return x.Length.CompareTo(y.Length)
        ElseIf x.Height.CompareTo(y.Height) <> 0 Then
            Return x.Height.CompareTo(y.Height)
        ElseIf x.Width.CompareTo(y.Width) <> 0 Then
            Return x.Width.CompareTo(y.Width)
        Else
            Return 0
        End If
    End Function

End Class

' This class is not demonstrated in the Main method
' and is provided only to show how to implement
' the interface. It is recommended to derive
' from Comparer<T> instead of implementing IComparer<T>.
Public Class BoxComp
    Implements IComparer(Of Box)
    ' Compares by Height, Length, and Width.
    Public Function Compare(ByVal x As Box, ByVal y As Box) As Integer Implements _
                                                IComparer(Of Box).Compare
        If x.Height.CompareTo(y.Height) <> 0 Then
            Return x.Height.CompareTo(y.Height)
        ElseIf x.Length.CompareTo(y.Length) <> 0 Then
            Return x.Length.CompareTo(y.Length)
        ElseIf x.Width.CompareTo(y.Width) <> 0 Then
            Return x.Width.CompareTo(y.Width)
        Else
            Return 0
        End If
    End Function
End Class

Public Class Box
    Implements IComparable(Of Box)

    Public Sub New(ByVal h As Integer, ByVal l As Integer, ByVal w As Integer)
        Me.Height = h
        Me.Length = l
        Me.Width = w
    End Sub
    Private privateHeight As Integer
    Public Property Height() As Integer
        Get
            Return privateHeight
        End Get
        Private Set(ByVal value As Integer)
            privateHeight = value
        End Set
    End Property
    Private privateLength As Integer
    Public Property Length() As Integer
        Get
            Return privateLength
        End Get
        Private Set(ByVal value As Integer)
            privateLength = value
        End Set
    End Property
    Private privateWidth As Integer
    Public Property Width() As Integer
        Get
            Return privateWidth
        End Get
        Private Set(ByVal value As Integer)
            privateWidth = value
        End Set
    End Property

    Public Function CompareTo(ByVal other As Box) As Integer _
                        Implements IComparable(Of Box).CompareTo
        ' Compares Height, Length, and Width.
        If Me.Height.CompareTo(other.Height) <> 0 Then
            Return Me.Height.CompareTo(other.Height)
        ElseIf Me.Length.CompareTo(other.Length) <> 0 Then
            Return Me.Length.CompareTo(other.Length)
        ElseIf Me.Width.CompareTo(other.Width) <> 0 Then
            Return Me.Width.CompareTo(other.Width)
        Else
            Return 0
        End If
    End Function

End Class

Poznámky

Odvodit z této třídy poskytnout vlastní implementaci IComparer<T> rozhraní pro použití s třídami kolekce, jako SortedList<TKey,TValue> jsou třídy a SortedDictionary<TKey,TValue> obecné třídy.

Rozdíl mezi odvozením z Comparer<T> třídy a implementací System.IComparable rozhraní je následující:

  • Chcete-li určit, jak se mají ve výchozím nastavení porovnávat dva objekty, implementujte System.IComparable rozhraní ve vaší třídě. Tím zajistíte, že operace řazení budou používat výchozí porovnávací kód, který jste zadali.

  • Chcete-li definovat porovnávač, který se má použít místo výchozího porovnávače, odvozujte z Comparer<T> třídy. Tento porovnávač pak můžete použít v operacích řazení, které jako parametr přebírají porovnávací nástroj.

Objekt vrácený Default vlastností používá System.IComparable<T> obecné rozhraní (IComparable<T> v jazyce C#, IComparable(Of T) v jazyce Visual Basic) k porovnání dvou objektů. Pokud typ T neimplementuje System.IComparable<T> obecné rozhraní, Default vrátí Comparer<T> vlastnost, která používá System.IComparable rozhraní.

Poznámky pro implementátory

Compare(T, T) a Equals(T, T) může se chovat odlišně z hlediska citlivosti jazykové verze a citlivosti na malá a velká písmena.

Pro porovnání řetězců se StringComparer třída doporučuje v průběhu Comparer<String>. StringComparer Vlastnosti třídy vrací předdefinované instance, které provádějí porovnání řetězců s různými kombinacemi citlivosti jazykové verze a citlivosti na malá a velká písmena. Citlivost na malá a velká písmena a citlivost jazykové verze jsou konzistentní mezi členy stejné StringComparer instance.

Další informace o porovnáních specifických pro jazykovou verzi najdete v System.Globalization oboru názvů a globalizaci a lokalizaci.

Konstruktory

Name Description
Comparer<T>()

Inicializuje novou instanci Comparer<T> třídy.

Vlastnosti

Name Description
Default

Vrátí výchozí porovnávač pořadí řazení pro typ určený obecným argumentem.

Metody

Name Description
Compare(T, T)

Při přepsání v odvozené třídě provede porovnání dvou objektů stejného typu a vrátí hodnotu určující, zda je jeden objekt menší, roven nebo větší než druhý.

Create(Comparison<T>)

Vytvoří porovnávač pomocí zadaného porovnání.

Equals(Object)

Určuje, zda je zadaný objekt roven aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetType()

Získá Type aktuální instance.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Object.

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

Name Description
IComparer.Compare(Object, Object)

Porovná dva objekty a vrátí hodnotu určující, zda je jedna menší než, rovna nebo větší než druhý.

Platí pro

Viz také