Export (0) Print
Expand All

SortKey.Compare Method

Compares two sort keys.

[Visual Basic]
Public Shared Function Compare( _
   ByVal sortkey1 As SortKey, _
   ByVal sortkey2 As SortKey _
) As Integer
[C#]
public static int Compare(
 SortKey sortkey1,
 SortKey sortkey2
);
[C++]
public: static int Compare(
 SortKey* sortkey1,
 SortKey* sortkey2
);
[JScript]
public static function Compare(
   sortkey1 : SortKey,
 sortkey2 : SortKey
) : int;

Parameters

sortkey1
The first sort key to compare.
sortkey2
The second sort key to compare.

Return Value

Value Condition
Zero The two sort keys are equal.
Less than zero sortkey1 is less than sortkey2.
Greater than zero sortkey1 is greater than sortkey2.

Remarks

Compare behaves the same way as CompareInfo.Compare. Unlike Equals, Compare is not affected by the CompareOptions setting of CompareInfo.GetSortKey. And, unlike Equals, Compare does not require that the SortKey parameters originate from the same System.Globalization.CompareInfo instances to be considered equal. Compare returns 0 if both SortKey instances have the same OriginalString and if OriginalString falls in the same sort order in the cultures of both SortKey instances.

Example

[Visual Basic, C#, C++] The following code example compares SortKey objects that are created using CultureInfo instances that have different sort orders.

[Visual Basic] 
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


[C#] 
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
*/


[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Creates a SortKey using the en-US culture.
   CultureInfo * MyCI = new CultureInfo(S"en-US", false);
   CompareInfo* myComp_enUS = MyCI -> CompareInfo;

   SortKey * mySK1 = myComp_enUS -> GetSortKey(S"llama");

   // Creates a SortKey using the es-ES culture with international sort.
   MyCI = new CultureInfo(S"es-ES", false);
   CompareInfo* myComp_esES = MyCI -> CompareInfo;

   SortKey * mySK2 = myComp_esES -> GetSortKey(S"llama");

   // Creates a SortKey using the es-ES culture with traditional sort.
   MyCI = new CultureInfo(0x040A, false);
   CompareInfo* myComp_es = MyCI -> CompareInfo;

   SortKey * mySK3 = myComp_es -> GetSortKey(S"llama");

   // Compares the en-US SortKey with each of the es-ES SortKey objects.
   Console::WriteLine(S"Comparing \"llama\" in en-US and in es-ES with international sort : {0}", 
      __box(SortKey::Compare(mySK1, mySK2)));
   Console::WriteLine(S"Comparing \"llama\" in en-US and in es-ES with traditional sort   : {0}", 
      __box(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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SortKey Class | SortKey Members | System.Globalization Namespace

Show:
© 2014 Microsoft