Overview Of CompareTo Function In C#

CompareTo

CompareTo function behaves differently for character, string and an integer.

Background

I was using CompareTo function. I observed that it does not work on ASCII values in case of string comparison. What is the base of CompareTo function? I was curious about this because I was not getting the desired result. I have googled but nothing find suitable. Then I decided to RND on CompareTo and found something noticeable.

For character

It returns the difference of ASCII values of both characters (calling object- passing object).

For string

It returns a 32-bit signed integer, which indicates the lexical relationship between the two objects.

(Basically it returns three values 0,1 and -1)

Let's see what is the meaning of 0, 1 and -1

Zero Value (0) - Both objects are equal.

Negative Value (-1) - Calling an object is smaller than passing an object in lexical order.

Positive Value (1) - Calling object is greater than passing object in lexical order.

For an integer

It compares the actual value and returns 0, if both the numbers are equal, -1, if calling an object is less than passing an object and 1 if calling an object is greater than passing an object.

Lexical Order

Lexical order is a generalized way of an alphabetical order. It basically defines the dictionary order of the words.

In lexical order, digits comes before letters and lower letters comes before uppercase.

Lexical order of number, small character and capital character is shown below. (When number is used as a string)

1,10,11,12,13,14,15,16,17,18,19 2,20,21.,22..... .. a, A, b, B, c, C, d, D, e, E,......

Any right item is greater than its left item i.e they are in ascending order.

For an integer 

  1. private void CompareInteger()  
  2.         {  
  3.             int iResult;  
  4.   
  5.             int First = 12;  
  6.             int Second = 12;  
  7.   
  8.             iResult = First.CompareTo(Second);  
  9.   
  10.             Console.WriteLine($"{First} - {Second}={iResult}");  
  11.   
  12.             First = 20;  
  13.             Second = 12;  
  14.   
  15.             iResult = First.CompareTo(Second);  
  16.   
  17.             Console.WriteLine($"{First} - {Second}={iResult}");  
  18.   
  19.             First = 12;  
  20.             Second = 20;  
  21.   
  22.             iResult = First.CompareTo(Second);  
  23.   
  24.             Console.WriteLine($"{First} - {Second}={iResult}");  
  25.         }   
  1. //Output  
  2.   
  3. 12 - 12 = 0  
  4.   
  5. 20 - 12 = 1  
  6.   
  7. 12 - 20 = 1   

Let's discuss the output. For an integer, it compares the actual value of an integer and returns 0,1 and -1 based on calling an object. It returns 0, if calling an object is equal to passing an object, returns 1; if calling object is greater, and -1 if calling object is smaller.

First ouput (12-12 =0) is obvious because both are equal.

Second output (20-12 =1) because 20 is greater than 12, so it returns 1.

Third output is (12-20 =-1) because 12 is smaller than 20, so it returns -1.

For character 

  1. private static void CompareCharacter()  
  2.         {  
  3.             int iResult;  
  4.   
  5.             char First = 'a';  
  6.             char Second = 'a';  
  7.   
  8.             iResult = First.CompareTo(Second);  
  9.   
  10.             Console.WriteLine($"{First} - {Second}={iResult}");  
  11.          
  12.   
  13.             First = 'a';  
  14.             Second = 'd';  
  15.   
  16.             iResult = First.CompareTo(Second);  
  17.   
  18.             Console.WriteLine($"{First} - {Second}={iResult}");  
  19.   
  20.             First = 'd';  
  21.             Second = 'a';  
  22.   
  23.             iResult = First.CompareTo(Second);  
  24.   
  25.             Console.WriteLine($"{First} - {Second}={iResult}");  
  26.         }   
  1. //Output  
  2.   
  3. a - a = 0  
  4.   
  5. a - d = -3  
  6.   
  7. d - a = 3   

ASCII value of a is 65 and ASCII value of d is 68, then 65-68 = -3

Let's discuss the output. For character, it returns difference of their ASCII value.

First ouput (a-a =0) is obvious because both have ASCII value 65.

Second output (a-d =-3) because (65-68 =-3).

Third output is (d-a =3) because (68-65 =3).

For a string 

  1. private static void CompareString()  
  2.         {  
  3.             int iResult;  
  4.   
  5.             String First = "a";  
  6.             String Second = "a";  
  7.   
  8.             iResult = First.CompareTo(Second);  
  9.   
  10.             Console.WriteLine($"{First} - {Second}={iResult}");  
  11.   
  12.   
  13.             First = "a";  
  14.             Second = "d";  
  15.   
  16.             iResult = First.CompareTo(Second);  
  17.   
  18.             Console.WriteLine($"{First} - {Second}={iResult}");  
  19.   
  20.             First = "d";  
  21.             Second = "a";  
  22.   
  23.             iResult = First.CompareTo(Second);  
  24.   
  25.             Console.WriteLine($"{First} - {Second}={iResult}");  
  26.   
  27.   
  28.             First = "A";  
  29.             Second = "a";  
  30.   
  31.             iResult = First.CompareTo(Second);  
  32.   
  33.             Console.WriteLine($"{First} - {Second}={iResult}");  
  34.   
  35.   
  36.             First = "A";  
  37.             Second = "d";  
  38.   
  39.             iResult = First.CompareTo(Second);  
  40.   
  41.             Console.WriteLine($"{First} - {Second}={iResult}");  
  42.   
  43.             First = "house";  
  44.             Second = "household";  
  45.   
  46.             iResult = First.CompareTo(Second);  
  47.   
  48.             Console.WriteLine($"{First} - {Second}={iResult}");  
  49.   
  50.             First = "composer";  
  51.             Second = "computer";  
  52.   
  53.             iResult = First.CompareTo(Second);  
  54.   
  55.             Console.WriteLine($"{First} - {Second}={iResult}");  
  56.               
  57.   
  58.             First = "H2O"; Second = "HOTEL";   
  59.             iResult = First.CompareTo(Second);   
  60.             Console.WriteLine($"{First} - {Second}={iResult}");  
  61.   
  62.             First = "10";   
  63.             Second = "2";   
  64.             iResult = First.CompareTo(Second);   
  65.             Console.WriteLine($"{First} - {Second}={iResult}");  
  66.   
  67.            First = "1";   
  68.            Second = "10";   
  69.            iResult = First.CompareTo(Second);   
  70.            Console.WriteLine($"{First} - {Second}={iResult}");  
  71.                                    
  72.         }   
  1. //Output  
  2.   
  3. a - a = 0  // because both are on same position in lexical order  
  4. a - d = -1 // because 'a' precede 'd' in lexical order i.e 'a' is smaller than 'd' according to lexical order so it returns -1.  
  5. d - a = 1  // because 'd' follows 'a'  in lexical order i.e 'd' is greater than 'a' according to lexical order.  
  6. A - a = 1  // because 'A' is greater than 'a' according to lexical order.   
  7. A - d = -1 // because 'A' is smaller than 'd' according to lexical order.  
  8. house - household = -1 // because house comes before household in dictionary or lexical order  
  9. composer - computer = -1 // because composer comes before computer in dictionary or lexical order  
  10. H2O - HOTEL = -1  // because H2O comes before computer in dictionary or lexical order  
  11. 10-2 = 1// because 10 comes before 2 in lexical order  
  12. 1-10 = 1 // because 1 comes before 10 in lexical order though 1 is smaller than 10 as an intger   
  13.           //but we have passed them as a string   
  14.   
  15. //If we discuss about composer and computer than we first four character are same  and fifth character is 'o' and 'u',   
  16. //if we compare both character than we will see that o comes before u in lexical order so composer will   
  17. //preceed computer in dictionary  
  18.   
  19. //Same is applicable for H2O and HOTEL , 2 preceed O, and for house and household house will preceed household. 

Let's discuss the output in detail. For string, it tells us lexical relationship between the objects.

house - household = -1, it returns 1 because house comes before household.

composer - computer = -1 , it starts comparing character by character, so first four charcters are same, while fifth charcter is different. Thus, it compares 'o' to 'u' and finds character 'o' comes before 'u' in lexical order. Thus, it returns -1.

H2O - HOTEL = -1 , it returns -1, first character is same and second character is different. Thus, it compares character '2' and character 'O' and finds that number comes before letter, so it returns -1.

Next Recommended Reading Overview Of Collection In C#