IComparer 介面
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
公開比較兩個物件的方法。
public interface class IComparer
C#
public interface IComparer
C#
[System.Runtime.InteropServices.ComVisible(true)]
public interface IComparer
type IComparer = interface
[<System.Runtime.InteropServices.ComVisible(true)>]
type IComparer = interface
Public Interface IComparer
- 衍生
- 屬性
下列範例會 IComparer 使用 介面來排序字串陣列。 在此範例中 Compare ,會使用 CaseInsensitiveComparer 類別來實作 方法,以反轉數位內容的順序。
C#
using System;
using System.Collections;
public class Example
{
public class ReverserClass : IComparer
{
// Call CaseInsensitiveComparer.Compare with the parameters reversed.
int IComparer.Compare(Object x, Object y)
{
return ((new CaseInsensitiveComparer()).Compare(y, x));
}
}
public static void Main()
{
// Initialize a string array.
string[] words = { "The", "quick", "brown", "fox", "jumps", "over",
"the", "lazy", "dog" };
// Display the array values.
Console.WriteLine("The array initially contains the following values:" );
PrintIndexAndValues(words);
// Sort the array values using the default comparer.
Array.Sort(words);
Console.WriteLine("After sorting with the default comparer:" );
PrintIndexAndValues(words);
// Sort the array values using the reverse case-insensitive comparer.
Array.Sort(words, new ReverserClass());
Console.WriteLine("After sorting with the reverse case-insensitive comparer:");
PrintIndexAndValues(words);
}
public static void PrintIndexAndValues(IEnumerable list)
{
int i = 0;
foreach (var item in list )
Console.WriteLine($" [{i++}]: {item}");
Console.WriteLine();
}
}
// The example displays the following output:
// The array initially contains the following values:
// [0]: The
// [1]: quick
// [2]: brown
// [3]: fox
// [4]: jumps
// [5]: over
// [6]: the
// [7]: lazy
// [8]: dog
//
// After sorting with the default comparer:
// [0]: brown
// [1]: dog
// [2]: fox
// [3]: jumps
// [4]: lazy
// [5]: over
// [6]: quick
// [7]: the
// [8]: The
//
// After sorting with the reverse case-insensitive comparer:
// [0]: the
// [1]: The
// [2]: quick
// [3]: over
// [4]: lazy
// [5]: jumps
// [6]: fox
// [7]: dog
// [8]: brown
Imports System.Collections
Public Class Example
Public Class ReverserClass : Implements IComparer
' Call CaseInsensitiveComparer.Compare with the parameters reversed.
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function
End Class
Public Shared Sub Main()
' Initialize a string array.
Dim words() As String = { "The", "quick", "brown", "fox", "jumps", "over",
"the", "lazy", "dog" }
' Display the array values.
Console.WriteLine("The array initially contains the following values:")
PrintIndexAndValues(words)
' Sort the array values of the ArrayList using the default comparer.
Array.Sort(words)
Console.WriteLine("After sorting with the default comparer:")
PrintIndexAndValues(words)
' Sort the array values using the reverse case-insensitive comparer.
Array.Sort(words, new ReverserClass())
Console.WriteLine("After sorting with the reverse case-insensitive comparer:")
PrintIndexAndValues(words)
End Sub
Public Shared Sub PrintIndexAndValues(list As IEnumerable)
Dim i As Integer = 0
For Each item In list
Console.WriteLine($" [{i}]: {item}")
i += 1
Next
Console.WriteLine()
End Sub
End Class
' The example displays the following output:
' The array initially contains the following values:
' [0]: The
' [1]: quick
' [2]: brown
' [3]: fox
' [4]: jumps
' [5]: over
' [6]: the
' [7]: lazy
' [8]: dog
'
' After sorting with the default comparer:
' [0]: brown
' [1]: dog
' [2]: fox
' [3]: jumps
' [4]: lazy
' [5]: over
' [6]: quick
' [7]: the
' [8]: The
'
' After sorting with the reverse case-insensitive comparer:
' [0]: the
' [1]: The
' [2]: quick
' [3]: over
' [4]: lazy
' [5]: jumps
' [6]: fox
' [7]: dog
' [8]: brown
這個介面會與 和 Array.BinarySearch 方法搭配Array.Sort使用。 它提供一種方式來自定義集合的排序順序。 Compare如需參數和傳回值的注意事項,請參閱 方法。 其泛型對等專案是 System.Collections.Generic.IComparer<T> 介面。
這個介面的預設實作是 Comparer 類別。 如需此介面的泛型版本,請參閱 System.Collections.Generic.IComparer<T>。
Compare(Object, Object) |
比較兩個物件並傳回值,指出其中一個物件為小於、等於或大於另一個物件。 |
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |