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

ソート‐キー【sort key】

読み方:そーときー

コンピューターでデータの並べ替え(ソート)をする際の、順序の基準となる要素や属性。五十音順、数字順など、優先順位をつけて複数設定することもできる。


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 API の LCMapString メソッドを 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) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortKey メンバ
System.Globalization 名前空間
CompareInfo.GetSortKey
KeyData

SortKey コンストラクタ ()


SortKey コンストラクタ (String, String, Boolean)

メモ : このコンストラクタは、.NET Framework version 2.0 で新しく追加されたものです。

SortKey(String,String,Boolean) コンストラクタは、指定した属性名、一致する規則、および並べ替え順序を使用して、SortKey クラスの新しいインスタンスを作成し、初期化します。

名前空間: System.DirectoryServices.Protocols
アセンブリ: System.DirectoryServices.Protocols (system.directoryservices.protocols.dll 内)
構文構文

Public Sub New ( _
    attributeName As String, _
    matchingRule As String, _
    reverseOrder As Boolean _
)
Dim attributeName As String
Dim matchingRule As String
Dim reverseOrder As Boolean

Dim instance As New SortKey(attributeName,
 matchingRule, reverseOrder)
public SortKey (
    string attributeName,
    string matchingRule,
    bool reverseOrder
)
public:
SortKey (
    String^ attributeName, 
    String^ matchingRule, 
    bool reverseOrder
)
public SortKey (
    String attributeName, 
    String matchingRule, 
    boolean reverseOrder
)
public function SortKey (
    attributeName : String, 
    matchingRule : String, 
    reverseOrder : boolean
)

パラメータ

attributeName

並べ替えキーとして使用する属性の名前。

matchingRule

並べ替えに対する一致する規則のオブジェクト識別子 (OID)。

reverseOrder

低い方から高い方への順序で並べ替える場合は true。高い方から低い方への順序で並べ替える場合は false。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SortKey クラス
SortKey メンバ
System.DirectoryServices.Protocols 名前空間

SortKey コンストラクタ


SortKey プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ AttributeName AttributeName プロパティは、並べ替えキーとして使用する属性の名前を指定します。
パブリック プロパティ MatchingRule MatchingRule プロパティは、並べ替えに対する一致する規則のオブジェクト識別子 (OID) を指定します。
パブリック プロパティ ReverseOrder ReverseOrder プロパティは、並べ替えが逆順で返されるかどうかを示します。
参照参照

関連項目

SortKey クラス
System.DirectoryServices.Protocols 名前空間

SortKey プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ KeyData 現在の SortKey オブジェクトを表すバイト配列を取得します。
パブリック プロパティ OriginalString 現在の SortKey オブジェクトを作成するために使用する元の文字列を取得します。
参照参照

関連項目

SortKey クラス
System.Globalization 名前空間
CompareInfo.GetSortKey
KeyData

SortKey メソッド


SortKey メソッド


SortKey メンバ

SortKey クラスは、並べ替えコントロールで使用する並べ替え条件を格納します。

SortKey データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ AttributeName AttributeName プロパティは、並べ替えキーとして使用する属性の名前を指定します。
パブリック プロパティ MatchingRule MatchingRule プロパティは、並べ替えに対する一致する規則のオブジェクト識別子 (OID) を指定します。
パブリック プロパティ ReverseOrder ReverseOrder プロパティは、並べ替えが逆順で返されるかどうかを示します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SortKey クラス
System.DirectoryServices.Protocols 名前空間

SortKey メンバ

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

SortKey データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ KeyData 現在の SortKey オブジェクトを表すバイト配列を取得します。
パブリック プロパティ OriginalString 現在の SortKey オブジェクトを作成するために使用する元の文字列を取得します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SortKey クラス
System.Globalization 名前空間
CompareInfo.GetSortKey
KeyData

ソートキー

(SortKey から転送)

出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2021/02/28 02:18 UTC 版)

ナビゲーションに移動 検索に移動

ソートキー (sort key)



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「SortKey」の関連用語

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

   

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



SortKeyのページの著作権

   
デジタル大辞泉デジタル大辞泉
(C)Shogakukan Inc.
株式会社 小学館
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.
ウィキペディアウィキペディア
All text is available under the terms of the GNU Free Documentation License.
この記事は、ウィキペディアのソートキー (改訂履歴)の記事を複製、再配布したものにあたり、GNU Free Documentation Licenseというライセンスの下で提供されています。 Weblio辞書に掲載されているウィキペディアの記事も、全てGNU Free Documentation Licenseの元に提供されております。

©2026 GRAS Group, Inc.RSS