Search code examples
c#stringstring-comparisonalphabetical

Can't compare strings by alphabetical order


My DB stores keys by alphabetical order:

-MmNI8oyb2QE_9V0WdaX \\1st (oldest)
-MmOAFDL9ZPD1gx4SjEU \\2nd
-MmPtIJ1LpFTRbweNWvD \\3rd
-MmPtd0IMuNIEYaPYPgZ \\4th (newest)

The sorting order on this list is correct you can check here that it's an alphabetical order. 1st string is the smallest, then 2nd, 3rd, and 4th.

I would like to compare every two strings from that list, and get the correct results like the order of that specific list, so if I compare the 4th string with the 3rd one, I'll receive a result that 4th is bigger than the 3rd.

What I tried so far: using string.Compare, however it doesn't work right for my specific list the results I received weren't consistent

string first="-MmNI8oyb2QE_9V0WdaX";
string second="-MmOAFDL9ZPD1gx4SjEU";
string third="-MmPtIJ1LpFTRbweNWvD";
string fourth="-MmPtd0IMuNIEYaPYPgZ";
string.Compare(third,fourth) //output: 1
string.Compare(second,third) //output: -1
string.Compare(first,second) //output: -1

(Output should all be either "1" or "-1". because my list is sorted) What function should I use instead of comparing the strings? I also tried string.Compare(3rd,4th,false) an overload which compares by the case but it didn't help, my guess is that it has to do something with the case


Solution

  • It seems you actually want to sort these strings by their ordinal value (their binary representation). In that case use StringComparison.Ordinal as comparisonType.

     string.Compare(third, fourth, StringComparison.Ordinal)); //output: -27
     string.Compare(second, third, StringComparison.Ordinal)); //output: -1
     string.Compare(first, second, StringComparison.Ordinal)); //output: -1
    

    Weirdly the first comparison yields -27 instead of -1. The Compare method only specifies that the return value will be <1, 0 or >1 so those three output are essentially (and semantically) the same result.