영어로 읽기

다음을 통해 공유


SortKey.Compare(SortKey, SortKey) 메서드

정의

두 정렬 키를 비교합니다.

public static int Compare (System.Globalization.SortKey sortkey1, System.Globalization.SortKey sortkey2);

매개 변수

sortkey1
SortKey

비교할 첫째 정렬 키입니다.

sortkey2
SortKey

비교할 둘째 정렬 키입니다.

반환

sortkey1과(와) sortkey2 간의 관계를 나타내는 부호 있는 정수입니다.

조건
0보다 작음 sortkey1sortkey2보다 작은 경우
0 sortkey1이(가) sortkey2와 같은 경우.
0보다 큼 sortkey1sortkey2보다 큰 경우

예외

sortkey1 또는 sortkey2null인 경우

예제

다음 코드 예제에서는 메서드와 동등한 CompareInfo.Compare(String, String, CompareOptions) 메서드를 Compare 사용하는 두 문자열을 비교합니다.

// This code example demonstrates the CompareInfo.Compare() and
// SortKey.Compare() methods.

using System;
using System.Globalization;

class Sample
{
    public static void Main()
    {
    string lowerABC = "abc";
    string upperABC = "ABC";
    int result = 0;

// Create a CompareInfo object for the en-US culture.
    Console.WriteLine("\nCreate a CompareInfo object for the en-US culture...\n");
    CompareInfo cmpi = CompareInfo.GetCompareInfo("en-US");
// Alternatively:
//  CompareInfo cmpi = new CultureInfo("en-US").CompareInfo;

// Create sort keys for lowercase and uppercase "abc", the en-US culture, and
// ignore case.
    SortKey sk1LowerIgnCase = cmpi.GetSortKey(lowerABC, CompareOptions.IgnoreCase);
    SortKey sk2UpperIgnCase = cmpi.GetSortKey(upperABC, CompareOptions.IgnoreCase);

// Create sort keys for lowercase and uppercase "abc", the en-US culture, and
// use case.
    SortKey sk1LowerUseCase = cmpi.GetSortKey(lowerABC, CompareOptions.None);
    SortKey sk2UpperUseCase = cmpi.GetSortKey(upperABC, CompareOptions.None);

// Compare lowercase and uppercase "abc", ignoring case and using CompareInfo.
    result = cmpi.Compare(lowerABC, upperABC, CompareOptions.IgnoreCase);
    Display(result, "CompareInfo, Ignore case", lowerABC, upperABC);
// Compare lowercase and uppercase "abc", ignoring case and using SortKey.
    result = SortKey.Compare(sk1LowerIgnCase, sk2UpperIgnCase);
    Display(result, "SortKey, Ignore case", lowerABC, upperABC);
    Console.WriteLine();

// Compare lowercase and uppercase "abc", using case and using CompareInfo.
    result = cmpi.Compare(lowerABC, upperABC, CompareOptions.None);
    Display(result, "CompareInfo, Use case", lowerABC, upperABC);
// Compare lowercase and uppercase "abc", using case and using SortKey.
    result = SortKey.Compare(sk1LowerUseCase, sk2UpperUseCase);
    Display(result, "SortKey, Use case", lowerABC, upperABC);
    }

// Display the results of a comparison.
    private static void Display(int compareResult, string title,
                                string lower, string upper)
    {
    string lessThan    = "less than ";
    string equalTo     = "equal to ";
    string greaterThan = "greater than ";
    string resultPhrase = null;
    string format = "{0}:\n    \"{1}\" is {2}\"{3}\".";

    if      (compareResult < 0) resultPhrase = lessThan;
    else if (compareResult > 0) resultPhrase = greaterThan;
    else                        resultPhrase = equalTo;
    Console.WriteLine(format, title, lower, resultPhrase, upper);
    }
}
/*
This code example produces the following results:

Create a CompareInfo object for the en-US culture...

CompareInfo, Ignore case:
    "abc" is equal to "ABC".
SortKey, Ignore case:
    "abc" is equal to "ABC".

CompareInfo, Use case:
    "abc" is less than "ABC".
SortKey, Use case:
    "abc" is less than "ABC".

*/

설명

메서드는 CompareKeyDatasortkey2 매개 변수의 sortkey1 속성을 비교합니다. 메서드는 메서드와 동일한 결과를 생성합니다 CompareInfo.Compare .

메서드 및 정렬 키 비교에 대한 Compare 자세한 내용은 클래스 항목을 참조 SortKey 하세요.

적용 대상

제품 버전
.NET 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 2.0, 2.1