SortKey クラスとは? わかりやすく解説

SortKey クラス


SortKey クラス

文字列とその並べ替えキーとの対応付け結果表します

名前空間: System.Globalization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class SortKey
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class SortKey
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class SortKey
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class SortKey
SerializableAttribute 
ComVisibleAttribute(true) 
public class SortKey
解説解説

カルチャに依存した 2 つ文字列比較は、スクリプトアルファベット順大文字と小文字区別発音区別など、並べ替えに関するさまざまな規則を持つ文字列内の文字依存します並べ替えキーは、特定の文字列対する、これらの規則リポジトリとして機能します。特に、SortKey オブジェクトの値はそのキー データです。これは、文字列エンコードする一連のバイト、カルチャ固有の並べ替え規則、およびユーザー指定比較オプションです。並べ替えキー使用した比較は、それぞれの並べ替えキー対応するキー データビット単位比較によって実現されます。

パフォーマンスに関する考慮事項

文字列比較を行う場合Compare メソッドと System.Globalization.CompareInfo.Compare メソッドは同じ結果生成します

実際において、System.Globalization.CompareInfo.Compare メソッドは、各文字列並べ替えキー生成し比較実行し並べ替えキー破棄して比較結果返します。実は、System.Globalization.CompareInfo.Compare メソッドは、並べ替えキー全体生成して比較実行するのではありません。その代わり、このメソッドは、各文字列内のそれぞれのテキスト要素 (つまり、基本文字サロゲート ペア、または組み合わせ文字シーケンス) のキー データ生成し対応するテキスト要素キー データ比較します。この操作は、比較最終結果求められる同時に終了します並べ替えキー情報計算されますが、SortKey オブジェクト作成されません。この方法は、両方文字列1 回だけ比較する場合パフォーマンス優れていますが、同じ文字列複数比較する場合高くつきます

Compare メソッドでは、比較実行する前に文字列ごとに SortKey オブジェクト生成する必要がありますこの方法は、SortKey オブジェクト生成するために時間メモリ投資されているので、1 回目比較パフォーマンスに関して高くつきます。しかし、同じ並べ替えキー複数比較する場合パフォーマンス良くなります

たとえば、文字列ベースインデックス列が指定され検索文字列一致する行をデータベース テーブルから検索するアプリケーション作成するとします。このテーブルには、数十万行格納されていて、検索文字列各行インデックス比較するにはかなりの時間要します。そこで、行とそのインデックス列を格納するときに、インデックス並べ替えキー同時に生成し検索パフォーマンス向上させるために用意され1 つの列にそのインデックス並べ替えキー格納します対象の行を検索するとき、アプリケーションは、検索文字列インデックス文字列とを比較する代わりに検索文字列並べ替えキーインデックス文字列並べ替えキーとを比較します。

セキュリティについての考慮事項

CompareInfo.GetSortKey(String,CompareOptions) メソッドは、指定した文字列と CompareOptions 値にその値が基づく SortKey オブジェクトと、基底の CompareInfo オブジェクト関連するカルチャを返しますセキュリティ上の判断文字列比較または大文字小文字変更する操作依存する場合は、インバリアント カルチャの CompareInfo.GetSortKey(String,CompareOptions) メソッド使用してシステムのカルチャ設定に関係なく動作一貫性保証されるようにしてください

GetSortKey メソッド取得するには、次の手順使用します

  1. CultureInfo.InvariantCulture プロパティ使用して、インバリアント カルチャを取得します

  2. インバリアント カルチャの CompareInfo プロパティ使用してCompareInfo オブジェクト取得します

  3. CompareInfo オブジェクトGetSortKey メソッド使用します

SortKey オブジェクトの値は、Windows APILCMapString メソッドLCMAP_SORTKEY フラグ付き呼び出すことと同じです。ただし、LCMapString違って英字並べ替えキー韓国語文字並べ替えキー優先されます。

並べ替えキー詳細については、Unicode Home Page の『Unicode Technical Standard #10』の「Unicode Collation Algorithm」を参照してください

使用例使用例

en-US および es-ES カルチャと、従来en-US および es-ES カルチャを使用した文字列 "llama" を比較する方法次のコード例示します

Imports System
Imports System.Globalization

Public Class SamplesSortKey

   Public Shared Sub Main()

      ' Creates a SortKey using the en-US culture.
      Dim myComp_enUS As CompareInfo = New
 CultureInfo("en-US", False).CompareInfo
      Dim mySK1 As SortKey = myComp_enUS.GetSortKey("llama")

      ' Creates a SortKey using the es-ES culture with international
 sort.
      Dim myComp_esES As CompareInfo = New
 CultureInfo("es-ES", False).CompareInfo
      Dim mySK2 As SortKey = myComp_esES.GetSortKey("llama")

      ' Creates a SortKey using the es-ES culture with traditional sort.
      Dim myComp_es As CompareInfo = New
 CultureInfo(&H40A, False).CompareInfo
      Dim mySK3 As SortKey = myComp_es.GetSortKey("llama")

      ' Compares the en-US SortKey with each of the es-ES SortKey objects.
      Console.WriteLine("Comparing ""llama""
 in en-US and in es-ES with international sort : {0}", SortKey.Compare(mySK1,
 mySK2))
      Console.WriteLine("Comparing ""llama""
 in en-US and in es-ES with traditional sort   : {0}", SortKey.Compare(mySK1,
 mySK3))

   End Sub 'Main 

End Class 'SamplesSortKey


'This code produces the following output.
'
'Comparing "llama" in en-US and in es-ES with international
 sort : 0
'Comparing "llama" in en-US and in es-ES with traditional
 sort   : -1

using System;
using System.Globalization;

public class SamplesSortKey  {

   public static void Main()
  {

      // Creates a SortKey using the en-US culture.
      CompareInfo myComp_enUS = new CultureInfo("en-US"
,false).CompareInfo;
      SortKey mySK1 = myComp_enUS.GetSortKey( "llama" );

      // Creates a SortKey using the es-ES culture with international
 sort.
      CompareInfo myComp_esES = new CultureInfo("es-ES"
,false).CompareInfo;
      SortKey mySK2 = myComp_esES.GetSortKey( "llama" );

      // Creates a SortKey using the es-ES culture with traditional
 sort.
      CompareInfo myComp_es   = new CultureInfo(0x040A,false).CompareInfo;
      SortKey mySK3 = myComp_es.GetSortKey( "llama" );

      // Compares the en-US SortKey with each of the es-ES SortKey objects.
      Console.WriteLine( "Comparing \"llama\" in
 en-US and in es-ES with international sort : {0}", SortKey.Compare(
 mySK1, mySK2 ) );
      Console.WriteLine( "Comparing \"llama\" in
 en-US and in es-ES with traditional sort   : {0}", SortKey.Compare(
 mySK1, mySK3 ) );

   }

}

/*
This code produces the following output.

Comparing "llama" in en-US and in
 es-ES with international sort : 0
Comparing "llama" in en-US and in
 es-ES with traditional sort   : -1
*/

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Creates a SortKey using the en-US culture.
   CultureInfo^ MyCI = gcnew CultureInfo( "en-US",false
 );
   CompareInfo^ myComp_enUS = MyCI->CompareInfo;
   SortKey^ mySK1 = myComp_enUS->GetSortKey( "llama" );
   
   // Creates a SortKey using the es-ES culture with international sort.
   MyCI = gcnew CultureInfo( "es-ES",false );
   CompareInfo^ myComp_esES = MyCI->CompareInfo;
   SortKey^ mySK2 = myComp_esES->GetSortKey( "llama" );
   
   // Creates a SortKey using the es-ES culture with traditional sort.
   MyCI = gcnew CultureInfo( 0x040A,false );
   CompareInfo^ myComp_es = MyCI->CompareInfo;
   SortKey^ mySK3 = myComp_es->GetSortKey( "llama" );
   
   // Compares the en-US SortKey with each of the es-ES SortKey objects.
   Console::WriteLine( "Comparing \"llama\" in
 en-US and in es-ES with international sort : {0}", SortKey::Compare(
 mySK1, mySK2 ) );
   Console::WriteLine( "Comparing \"llama\" in
 en-US and in es-ES with traditional sort   : {0}", SortKey::Compare(
 mySK1, mySK3 ) );
}

/*
This code produces the following output.

Comparing S"llama" in en-US and in
 es-ES with international sort : 0
Comparing S"llama" in en-US and in
 es-ES with traditional sort   : -1
*/
import System.*;
import System.Globalization.*;

public class SamplesSortKey
{   
    public static void main(String[]
 args)
    {
        // Creates a SortKey using the en-US culture.
        CompareInfo myComp_enUS = 
            (new CultureInfo("en-US", false)).get_CompareInfo();
        SortKey mySK1 = myComp_enUS.GetSortKey("llama");

        // Creates a SortKey using the es-ES culture with international
 sort.
        CompareInfo myComp_esES = 
            (new CultureInfo("es-ES", false)).get_CompareInfo();
        SortKey mySK2 = myComp_esES.GetSortKey("llama");

        // Creates a SortKey using the es-ES culture with traditional
 sort.
        CompareInfo myComp_es = 
            (new CultureInfo(0x40A, false)).get_CompareInfo();
        SortKey mySK3 = myComp_es.GetSortKey("llama");

        // Compares the en-US SortKey with each of the es-ES SortKey
 objects.
        Console.WriteLine("Comparing \"llama\" in
 en-US and in es-ES with"
            + " international sort : {0}",
            System.Convert.ToString( SortKey.Compare(mySK1, mySK2)));
        Console.WriteLine("Comparing \"llama\" in
 en-US and in es-ES with"
            + " traditional sort   : {0}",
            System.Convert.ToString( SortKey.Compare(mySK1, mySK3)));
    } //main
} //SamplesSortKey

/*
This code produces the following output.

Comparing "llama" in en-US and in
 es-ES with international sort : 0
Comparing "llama" in en-US and in
 es-ES with traditional sort   : -1
*/

継承階層継承階層
System.Object
  System.Globalization.SortKey
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「SortKey クラス」の関連用語

SortKey クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SortKey クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS