Comparer クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public NotInheritable Class Comparer Implements IComparer, ISerializable
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class Comparer : IComparer, ISerializable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Comparer sealed : IComparer, ISerializable

このクラスは、IComparer インターフェイスの既定の実装です。CaseInsensitiveComparer クラスは、大文字と小文字を区別しない文字列比較を実行する IComparer インターフェイスの実装です。
比較プロシージャは、その他の方法で指定されない限りは、現在のスレッドの Thread.CurrentCulture を使用します。文字列比較の結果は、カルチャに応じて異なる場合があります。カルチャ固有の比較の詳細については、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 */

System.Collections.Comparer


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 コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)


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 コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)



比較プロシージャは、並べ替え順序と大文字と小文字の規則を決定するために、指定した System.Globalization.CultureInfo を使用します。文字列比較の結果は、カルチャに応じて異なる場合があります。カルチャ固有の比較の詳細については、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 クラス
Comparer メンバ
System.Collections 名前空間
System.Globalization.CultureInfo
System.Globalization.CompareInfo
Comparer ジェネリック クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<SerializableAttribute> _ Public MustInherit Class Comparer(Of T) Implements IComparer, IComparer(Of T)
[SerializableAttribute] generic<typename T> public ref class Comparer abstract : IComparer, IComparer<T>

このクラスから派生して、SortedList ジェネリック クラスや SortedDictionary ジェネリック クラスなどのコレクション クラスで使用される IComparer インターフェイスのカスタムの実装を提供します。
Default プロパティによって返されるオブジェクトでは、System.IComparable ジェネリック インターフェイス (C# では IComparable<T>、Visual Basic では IComparable(Of T)) を使用して 2 つのオブジェクトが比較されます。T 型が System.IComparable ジェネリック インターフェイスを実装していない場合、Default プロパティは、System.IComparable インターフェイスを使用する Comparer を返します。
実装時の注意 Compare と EqualityComparer.Equals は、カルチャの依存性、および大文字小文字の区別の点で動作が異なる場合があります。 文字列比較の場合は、Comparer<String> に StringComparer クラスを使用することをお勧めします。StringComparer クラスのプロパティは、カルチャの依存性や大文字と小文字の区別をさまざまに組み合わせることによって文字列比較を実行する、定義済みのインスタンスを返します。大文字小文字の区別およびカルチャの依存性は、StringComparer の同じインスタンスのすべてのメンバで共有されます。 カルチャ固有の比較の詳細については、System.Globalization 名前空間のトピックおよび「エンコーディングとローカリゼーション」を参照してください。
System.Collections.Generic.Comparer


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 メンバ
System.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo
Comparer フィールド

名前 | 説明 | |
---|---|---|
![]() | DefaultInvariant | CultureInfo.InvariantCulture に関連付けられた Comparer のインスタンスを表します。このフィールドは読み取り専用です。 |

関連項目
Comparer クラスSystem.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo
Comparer プロパティ


関連項目
Comparer ジェネリック クラスSystem.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo
Comparer メソッド

名前 | 説明 | |
---|---|---|
![]() | Compare | 派生クラスでオーバーライドされると、同じ型の 2 つのオブジェクトに対する比較を実行し、一方のオブジェクトが他方よりも小さいか、等しいか、大きいかを示す値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |


関連項目
Comparer ジェネリック クラスSystem.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo
Comparer メソッド

名前 | 説明 | |
---|---|---|
![]() | Compare | 同じ型の 2 つのオブジェクトに対して大文字と小文字を区別する比較を実行し、一方が他方よりも小さいか、等しいか、大きいかを示す値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetObjectData | SerializationInfo オブジェクトに、シリアル化に必要なデータを設定します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

関連項目
Comparer クラスSystem.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo
Comparer メンバ
IComparer ジェネリック インターフェイスの実装のための基本クラスを提供します。
Comparer ジェネリック型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Compare | 派生クラスでオーバーライドされると、同じ型の 2 つのオブジェクトに対する比較を実行し、一方のオブジェクトが他方よりも小さいか、等しいか、大きいかを示す値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |


関連項目
Comparer ジェネリック クラスSystem.Collections.Generic 名前空間
IComparer
IComparable ジェネリック インターフェイス
StringComparer クラス
System.Globalization.CultureInfo
Comparer メンバ
2 つのオブジェクトが等しいかどうかを比較します。文字列の大文字と小文字は区別されます。
Comparer データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | DefaultInvariant | CultureInfo.InvariantCulture に関連付けられた Comparer のインスタンスを表します。このフィールドは読み取り専用です。 |

名前 | 説明 | |
---|---|---|
![]() | Compare | 同じ型の 2 つのオブジェクトに対して大文字と小文字を区別する比較を実行し、一方が他方よりも小さいか、等しいか、大きいかを示す値を返します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetObjectData | SerializationInfo オブジェクトに、シリアル化に必要なデータを設定します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

関連項目
Comparer クラスSystem.Collections 名前空間
IComparer
IComparable インターフェイス
CaseInsensitiveComparer クラス
System.Collections.Generic.Comparer
System.Globalization.CompareInfo
Thread.CurrentCulture
System.Globalization.CultureInfo
- Comparerのページへのリンク