Comparer.Compare メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Dim instance As Comparer(Of T) Dim x As T Dim y As T Dim returnValue As Integer returnValue = instance.Compare(x, y)
戻り値
T 型の並べ替え順序の比較方法をカスタマイズするには、このメソッドを実装します。
実装時の注意 null 参照 (Visual Basic では Nothing) を任意の参照型と比較できます。このような比較を行っても例外が生成されることはありません。null 参照は、null 以外のすべての参照より小さいと見なされます。 カルチャ固有の比較の詳細については、System.Globalization 名前空間のトピックおよび「エンコーディングとローカリゼーション」を参照してください。Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Comparer ジェネリック クラス
Comparer メンバ
System.Collections.Generic 名前空間
IComparable ジェネリック インターフェイス
IComparable インターフェイス
StringComparer クラス
Object クラス
System.Globalization.CultureInfo
Comparer.Compare メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Dim instance As Comparer Dim a As Object Dim b As Object Dim returnValue As Integer returnValue = instance.Compare(a, b)
戻り値
a が IComparable を実装する場合は、a .CompareTo ( b ) が返されます。逆に、b が IComparable を実装する場合は、符号反転した b .CompareTo ( a ) が返されます。
null 参照 (Visual Basic では Nothing) を任意の型と比較できます。このような比較を行っても、IComparable を使用した場合に例外が生成されることはありません。並べ替え処理では、null 参照 (Visual Basic では Nothing) は、他のすべてのオブジェクトより小さいと見なされます。
文字列比較の結果は、カルチャに応じて異なる場合があります。カルチャ固有の比較の詳細については、System.Globalization 名前空間のトピックおよび「エンコーディングとローカリゼーション」を参照してください。
Comparer に関連付けられているカルチャに応じて、Compare が返す値が変わる方法については、次のコードの例を参照してください。
Imports System Imports System.Collections Imports System.Globalization Public Class SamplesComparer Public Shared Sub Main() ' Creates the strings to compare. Dim str1 As [String] = "llegar" Dim str2 As [String] = "lugar" Console.WriteLine("Comparing ""{0}"" and ""{1}"" ...", str1, str2) ' Uses the DefaultInvariant Comparer. Console.WriteLine(" Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare(str1, str2)) ' Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort). Dim myCompIntl As New Comparer(New CultureInfo("es-ES", False)) Console.WriteLine(" International Sort: {0}", myCompIntl.Compare(str1, str2)) ' Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort). Dim myCompTrad As New Comparer(New CultureInfo(&H40A, False)) Console.WriteLine(" Traditional Sort : {0}", myCompTrad.Compare(str1, str2)) End Sub 'Main End Class 'SamplesComparer 'This code produces the following output. ' 'Comparing "llegar" and "lugar" ... ' Invariant Comparer: -1 ' International Sort: -1 ' Traditional Sort : 1
using System; using System.Collections; using System.Globalization; public class SamplesComparer { public static void Main() { // Creates the strings to compare. String str1 = "llegar"; String str2 = "lugar"; Console.WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 ); // Uses the DefaultInvariant Comparer. Console.WriteLine( " Invariant Comparer: {0}", Comparer.DefaultInvariant.Compare( str1, str2 ) ); // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort). Comparer myCompIntl = new Comparer( new CultureInfo( "es-ES", false ) ); Console.WriteLine( " International Sort: {0}", myCompIntl.Compare( str1, str2 ) ); // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort). Comparer myCompTrad = new Comparer( new CultureInfo( 0x040A, false ) ); Console.WriteLine( " Traditional Sort : {0}", myCompTrad.Compare( str1, str2 ) ); } } /* This code produces the following output. Comparing "llegar" and "lugar" ... Invariant Comparer: -1 International Sort: -1 Traditional Sort : 1 */
using namespace System; using namespace System::Collections; using namespace System::Globalization; int main() { // Creates the strings to compare. String^ str1 = "llegar"; String^ str2 = "lugar"; Console::WriteLine( "Comparing \"{0}\" and \"{1}\" ...", str1, str2 ); // Uses the DefaultInvariant Comparer. Console::WriteLine( " Invariant Comparer: {0}", Comparer::DefaultInvariant->Compare( str1, str2 ) ); // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, international sort). Comparer^ myCompIntl = gcnew Comparer( gcnew CultureInfo( "es-ES",false ) ); Console::WriteLine( " International Sort: {0}", myCompIntl->Compare( str1, str2 ) ); // Uses the Comparer based on the culture identifier 0x040A (Spanish - Spain, traditional sort). Comparer^ myCompTrad = gcnew Comparer( gcnew CultureInfo( 0x040A,false ) ); Console::WriteLine( " Traditional Sort : {0}", myCompTrad->Compare( str1, str2 ) ); } /* This code produces the following output. Comparing "llegar" and "lugar" ... Invariant Comparer: -1 International Sort: -1 Traditional Sort : 1 */
import System.* ; import System.Collections.*; import System.Globalization.*; public class SamplesComparer { public static void main(String[] args) { // Creates the strings to compare. String str1 = "llegar"; String str2 = "lugar"; Console.WriteLine("Comparing \"{0}\" and \"{1}\" ...", str1, str2); // Uses the DefaultInvariant Comparer. Console.WriteLine(" Invariant Comparer: {0}", (Int32)Comparer.DefaultInvariant.Compare(str1, str2)); // Uses the Comparer based on the culture "es-ES" (Spanish - Spain, // international sort). Comparer myCompIntl = new Comparer(new CultureInfo("es-ES", false)); Console.WriteLine(" International Sort: {0}", (Int32)myCompIntl.Compare(str1, str2)); // Uses the Comparer based on the culture identifier 0x040A // (Spanish - Spain, traditional sort). Comparer myCompTrad = new Comparer(new CultureInfo(0x40A, false)); Console.WriteLine(" Traditional Sort : {0}", (Int32)myCompTrad.Compare(str1, str2)); } //main } //SamplesComparer /* This code produces the following output. Comparing "llegar" and "lugar" ... Invariant Comparer: -1 International Sort: -1 Traditional Sort : 1 */
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Comparer.Compare メソッドのページへのリンク