Solitaire
Well-known member
If String.Compare uses the ASCII/Unicode values, then how come it places the lowercase values higher than the uppercase? It only works correctly when comparing the same case with different letters. Changing the CultureInfo makes no difference. Using relational operators to compare upper with lowercase strings or same case strings works correctly with the ASCII values.
Why is it not using the ASCII/Unicode values? This can be confusing to my students after explaining that data is sorted using ASCII values and showing them how it works with relational operators.
Furthermore, using the Array.Sort method with upper and lowercase letters returns bizarre results. For example,
The result is: a A b B c C
It should sort all the lowercase and uppercase letters separately. Is this some kind of bug?
VB.NET:
Sub Main()
Dim first, second As String
first = "A"
second = "a"
Console.WriteLine("Using relational operators:")
If first > second Then
Console.WriteLine(first & " > " & second)
ElseIf first < second Then
Console.WriteLine(first & " < " & second)
Else
Console.WriteLine(first & " = " & second)
End If
Console.WriteLine(vbCrLf & "Using String.Compare: (places lowercase first)")
Console.WriteLine("Value returned is " & String.Compare(first, second))
If String.Compare(first, second, False) > 0 Then
Console.WriteLine(first & " > " & second)
ElseIf String.Compare(first, second, False) < 0 Then
Console.WriteLine(first & " < " & second)
Else
Console.WriteLine(first & " = " & second)
End If
'Using String.Compare with True (case insensitive) finds them equal.
Console.ReadLine()
End Sub
Why is it not using the ASCII/Unicode values? This can be confusing to my students after explaining that data is sorted using ASCII values and showing them how it works with relational operators.
Furthermore, using the Array.Sort method with upper and lowercase letters returns bizarre results. For example,
VB.NET:
Sub Main()
Dim stringarray() As String = {"a", "b", "C", "A", "B", "c"}
Array.Sort(stringarray)
For x As Integer = 0 To 5
Console.WriteLine(stringarray(x))
Next x
Console.ReadLine()
End Sub
The result is: a A b B c C
It should sort all the lowercase and uppercase letters separately. Is this some kind of bug?