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
- private void CompareInteger()
- {
- int iResult;
-
- int First = 12;
- int Second = 12;
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = 20;
- Second = 12;
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = 12;
- Second = 20;
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
- }
-
-
- 12 - 12 = 0
-
- 20 - 12 = 1
-
- 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
- private static void CompareCharacter()
- {
- int iResult;
-
- char First = 'a';
- char Second = 'a';
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
-
- First = 'a';
- Second = 'd';
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = 'd';
- Second = 'a';
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
- }
-
-
- a - a = 0
-
- a - d = -3
-
- 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
- private static void CompareString()
- {
- int iResult;
-
- String First = "a";
- String Second = "a";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
-
- First = "a";
- Second = "d";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = "d";
- Second = "a";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
-
- First = "A";
- Second = "a";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
-
- First = "A";
- Second = "d";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = "house";
- Second = "household";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = "composer";
- Second = "computer";
-
- iResult = First.CompareTo(Second);
-
- Console.WriteLine($"{First} - {Second}={iResult}");
-
-
- First = "H2O"; Second = "HOTEL";
- iResult = First.CompareTo(Second);
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = "10";
- Second = "2";
- iResult = First.CompareTo(Second);
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- First = "1";
- Second = "10";
- iResult = First.CompareTo(Second);
- Console.WriteLine($"{First} - {Second}={iResult}");
-
- }
-
-
- a - a = 0
- a - d = -1
- d - a = 1
- A - a = 1
- A - d = -1
- house - household = -1
- composer - computer = -1
- H2O - HOTEL = -1
- 10-2 = 1
- 1-10 = 1
-
-
-
-
-
-
-
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.