Results 1 to 3 of 3

Thread: [RESOLVED] C#'s string.Compare vs VB.NET's Operators.CompareString

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    199

    Resolved [RESOLVED] C#'s string.Compare vs VB.NET's Operators.CompareString

    Code:
    using Microsoft.VisualBasic.CompilerServices;
    Code:
    int a = string.Compare("Davis", "DAVIS", false); //-1
    int b = Operators.CompareString("Davis", "DAVIS", false); //1
    int c= string.Compare("Peter", "Davis", false); //1   I expect 1. OK.  
    int d= Operators.CompareString("Peter", "Davis", false); //1 
    int f= Operators.CompareString("Peter", "davis", false); //-1
    I expect a =1 because "a" is greater than "A". But c returns 1 is what I expect. Operators.CompareString always returns what I expect, e.g.b and d return 1, f returns -1.

    Please explain what is C#'s string.Compare difference with Operators.CompareString.
    Last edited by DaveDavis; Jan 10th, 2018 at 08:55 AM.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: C#'s string.Compare vs VB.NET's Operators.CompareString

    First, to be clear, it isn't "C#'s string.Compare". That is "Microsoft's .NET BCL method String.Compare()". All .NET languages have access to it, it's defined probably in the mscorlib.dll assembly.

    What's happening here is several problems. First, you're misinterpreting how sorting works, or unaware there are two ways to sort letters in English.

    I expect a =1 because "a" is greater than "A".
    That is only true if you do an ORDINAL comparison of the letters. That is sometimes called "ASCIIbetical" sorting, because it involves using the value of the character in ASCII or some other encoding to determine how it sorts.

    But there are rules in English for sorting characters, and similar rules in other languages. If you don't specifically tell String.Compare() what to do, it will always use culture-appropriate sorting. In English, capital letters always sort before lowercase letters, so "A" is less than "a" in terms of English sorting rules.

    So let's walk through what each line does.

    Code:
    int a = string.Compare("Davis", "DAVIS", false); //-1
    This is a case-insensitive comparison that uses the current culture for sorting, English on my machine. "D" and "D" are identical, so "a" and "A" are compared. In English, "Apple" is supposed to sort before "apple". So since "Da" sorts less than "DA", "a" < "A" and -1 is returned.

    Code:
    int b = Operators.CompareString("Davis", "DAVIS", false); //1
    This is interesting. Operators.CompareString() has two neat things to say:
    This API supports the product infrastructure and is not intended to be used directly from your code.
    This performs a case-sensitive comparison that is either ordinal or based on the current culture, depending on the value of the statement.
    For whatever reason, your line is meeting the "depending on the value of the statement" that causes Operators.CompareString() to perform ordinal comparison. That means it decides since "a" is 97 and "A" is 65, "a" > "A" and returns 1.

    Unfortunately, I can't find reference source for Operators.CompareString() so I can't tell you why it decides to do an ordinal comparison here. This is probably why the documentation suggests you shouldn't call it directly. If you want to compare Strings in .NET, you should use String.Compare. It has an overload that lets you specify when you want to use ordinal comparison, but defaults to culture-specific comparison.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2016
    Posts
    199

    Re: C#'s string.Compare vs VB.NET's Operators.CompareString

    Thanks Mr.Sitten. Your reply is very detailed.
    The reason I used Operators.CompareString() is because the original code written in VB6 then VB.NET, now Boss ask for C#. I will take note on that.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width