Export (0) Print
Expand All

SortKey.Compare Method

Compares two sort keys.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function Compare ( _
	sortkey1 As SortKey, _
	sortkey2 As SortKey _
) As Integer
'Usage
Dim sortkey1 As SortKey 
Dim sortkey2 As SortKey 
Dim returnValue As Integer 

returnValue = SortKey.Compare(sortkey1, _
	sortkey2)

Parameters

sortkey1
Type: System.Globalization.SortKey

The first sort key to compare.

sortkey2
Type: System.Globalization.SortKey

The second sort key to compare.

Return Value

Type: System.Int32

Value

Condition

Less than zero

sortkey1 is less than sortkey2.

Zero

sortkey1 is equal to sortkey2.

Greater than zero

sortkey1 is greater than sortkey2.

ExceptionCondition
ArgumentNullException

sortkey1 or sortkey2 is Nothing.

The Compare method compares the KeyData properties of the sortkey1 and sortkey2 parameters. The method yields the same results as the CompareInfo.Compare method.

The following code example compares two strings using the Compare method and the equivalent CompareInfo.Compare(String, String, CompareOptions) method.

' This code example demonstrates the CompareInfo.Compare() and 
' SortKey.Compare() methods. 

Imports System
Imports System.Globalization

Class Sample
    Public Shared Sub Main() 
        Dim lowerABC As String = "abc" 
        Dim upperABC As String = "ABC" 
        Dim result As Integer = 0

    ' Create a CompareInfo object for the en-US culture.
        Console.WriteLine(vbCrLf & _
                          "Create a CompareInfo object for the en-US culture..." & _
                          vbCrLf)
        Dim cmpi As CompareInfo = CompareInfo.GetCompareInfo("en-US")
    ' Alternatively: 
    '   Dim cmpi As CompareInfo = New CultureInfo("en-US").CompareInfo 

    ' Create sort keys for lowercase and uppercase "abc", the en-US culture, and  
    ' ignore case.  
        Dim sk1LowerIgnCase As SortKey = cmpi.GetSortKey(lowerABC, CompareOptions.IgnoreCase)
        Dim sk2UpperIgnCase As SortKey = cmpi.GetSortKey(upperABC, CompareOptions.IgnoreCase)

    ' Create sort keys for lowercase and uppercase "abc", the en-US culture, and  
    ' use case.  
        Dim sk1LowerUseCase As SortKey = cmpi.GetSortKey(lowerABC, CompareOptions.None)
        Dim sk2UpperUseCase As SortKey = cmpi.GetSortKey(upperABC, CompareOptions.None)

    ' Compare lowercase and uppercase "abc", ignoring case and using CompareInfo.
        result = cmpi.Compare(lowerABC, upperABC, CompareOptions.IgnoreCase)
        Display(result, "CompareInfo, Ignore case", lowerABC, upperABC)
    ' Compare lowercase and uppercase "abc", ignoring case and using SortKey.
        result = SortKey.Compare(sk1LowerIgnCase, sk2UpperIgnCase)
        Display(result, "SortKey, Ignore case", lowerABC, upperABC)
        Console.WriteLine()

    ' Compare lowercase and uppercase "abc", using case and using CompareInfo.
        result = cmpi.Compare(lowerABC, upperABC, CompareOptions.None)
        Display(result, "CompareInfo, Use case", lowerABC, upperABC)
    ' Compare lowercase and uppercase "abc", using case and using SortKey.
        result = SortKey.Compare(sk1LowerUseCase, sk2UpperUseCase)
        Display(result, "SortKey, Use case", lowerABC, upperABC)
    End Sub 'Main

    ' Display the results of a comparison. 
    Private Shared Sub Display(ByVal compareResult As Integer, _
                               ByVal title As String, _
                               ByVal lower As String, _
                               ByVal upper As String) 
        Dim lessThan As String = "less than " 
        Dim equalTo As String = "equal to " 
        Dim greaterThan As String = "greater than " 
        Dim resultPhrase As String = Nothing 
        Dim format As String = "{0}:" & vbCrLf & "    ""{1}"" is {2}""{3}""." 

        If compareResult < 0 Then
            resultPhrase = lessThan
        ElseIf compareResult > 0 Then
            resultPhrase = greaterThan
        Else
            resultPhrase = equalTo
        End If
        Console.WriteLine(format, title, lower, resultPhrase, upper)
    End Sub 'Display
End Class 'Sample


'This code example produces the following results: 

'Create a CompareInfo object for the en-US culture... 

'CompareInfo, Ignore case: 
'    "abc" is equal to "ABC". 
'SortKey, Ignore case: 
'    "abc" is equal to "ABC". 

'CompareInfo, Use case: 
'    "abc" is less than "ABC". 
'SortKey, Use case: 
'    "abc" is less than "ABC". 
'

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft