SortKey.Compare(SortKey, SortKey) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
2 つの並べ替えキーを比較します。
public:
static int Compare(System::Globalization::SortKey ^ sortkey1, System::Globalization::SortKey ^ sortkey2);
public static int Compare (System.Globalization.SortKey sortkey1, System.Globalization.SortKey sortkey2);
static member Compare : System.Globalization.SortKey * System.Globalization.SortKey -> int
Public Shared Function Compare (sortkey1 As SortKey, sortkey2 As SortKey) As Integer
パラメーター
- sortkey1
- SortKey
比較対象の第 1 並べ替えキー。
- sortkey2
- SortKey
比較対象の第 2 並べ替えキー。
戻り値
sortkey1
および sortkey2
の間のリレーションシップを示す符号付き整数。
[値] | 条件 |
---|---|
0 より小さい値 | sortkey1 は sortkey2 より小さい値です。
|
ゼロ | sortkey1 と sortkey2 が等価です。
|
0 より大きい値 | sortkey1 が sortkey2 より大きくなっています。
|
例外
sortkey1
または sortkey2
が null
です。
例
次のコード例では、 メソッドと同等CompareInfo.Compare(String, String, CompareOptions)のメソッドをCompare使用して 2 つの文字列を比較します。
// 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".
*/
' This code example demonstrates the CompareInfo.Compare() and
' SortKey.Compare() methods.
Imports System.Globalization
Class Sample
Public Shared Sub Main()
Dim lowerABC As String = "abc"
Dim upperABC As String = "ABC"
Dim result As Integer = 0
' Create a CompareInfo object for the en-US culture.
Console.WriteLine(vbCrLf & _
"Create a CompareInfo object for the en-US culture..." & _
vbCrLf)
Dim cmpi As CompareInfo = CompareInfo.GetCompareInfo("en-US")
' Alternatively:
' Dim cmpi As CompareInfo = New CultureInfo("en-US").CompareInfo
' Create sort keys for lowercase and uppercase "abc", the en-US culture, and
' ignore case.
Dim sk1LowerIgnCase As SortKey = cmpi.GetSortKey(lowerABC, CompareOptions.IgnoreCase)
Dim sk2UpperIgnCase As SortKey = cmpi.GetSortKey(upperABC, CompareOptions.IgnoreCase)
' Create sort keys for lowercase and uppercase "abc", the en-US culture, and
' use case.
Dim sk1LowerUseCase As SortKey = cmpi.GetSortKey(lowerABC, CompareOptions.None)
Dim sk2UpperUseCase As SortKey = 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)
End Sub
' Display the results of a comparison.
Private Shared Sub Display(ByVal compareResult As Integer, _
ByVal title As String, _
ByVal lower As String, _
ByVal upper As String)
Dim lessThan As String = "less than "
Dim equalTo As String = "equal to "
Dim greaterThan As String = "greater than "
Dim resultPhrase As String = Nothing
Dim format As String = "{0}:" & vbCrLf & " ""{1}"" is {2}""{3}""."
If compareResult < 0 Then
resultPhrase = lessThan
ElseIf compareResult > 0 Then
resultPhrase = greaterThan
Else
resultPhrase = equalTo
End If
Console.WriteLine(format, title, lower, resultPhrase, upper)
End Sub
End Class
'
'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".
'
注釈
メソッドはCompare、 パラメーターと sortkey2
パラメーターのプロパティをsortkey1
比較KeyDataします。 メソッドは、 メソッドと同じ結果を CompareInfo.Compare 生成します。
メソッドと並べ替えキーの比較の詳細 Compare については、クラスのトピックを SortKey 参照してください。
適用対象
GitHub で Microsoft と共同作業する
このコンテンツのソースは GitHub にあります。そこで、issue や pull request を作成および確認することもできます。 詳細については、共同作成者ガイドを参照してください。
.NET