IComparer<T>.Compare(T, T) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
두 개체를 비교하고 한 개체가 다른 개체보다 작거나 같은지 또는 다른 개체보다 큰지를 나타내는 값을 반환합니다.
public:
int Compare(T x, T y);
public int Compare(T x, T y);
abstract member Compare : 'T * 'T -> int
Public Function Compare (x As T, y As T) As Integer
매개 변수
- x
- T
비교할 첫 번째 개체입니다.
- y
- T
비교할 두 번째 개체입니다.
반품
다음 표와 같이 상대 값을 x 나타내는 부가 정수입니다 y.
| 값 | 의미 |
|---|---|
| 0보다 작음 |
x 가 .보다 y작습니다.
|
| 0 |
x같음 .y
|
| 0보다 큼 |
x이 y보다 큽니다.
|
예제
다음 예제에서는 해당 차원에 IComparer<T> 따라 형식 Box 의 개체를 비교 하는 인터페이스를 구현 합니다. 이 예제는 클래스에 제공된 더 큰 예제의 Comparer<T> 일부입니다.
// 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;
}
}
}
' 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
설명
형식에 대해 사용자 지정된 정렬 순서 비교를 제공하려면 이 메서드를 구현합니다 T.
null 참조 형식과 비교할 수 있으며 예외를 생성하지 않습니다. null 참조는 null이 아닌 참조보다 작은 것으로 간주됩니다.