Note: If the first string is bigger, the result is 1. If the first string is smaller, the result is -1.
And: If both strings are equal, the result is 0. The number essentially indicates how much "larger" the first string is.
C# program that uses Compare
static void Main()
string a = "a";
string b = "b";
int c = string.Compare(a, b);
c = string.CompareOrdinal(b, a);
c = a.CompareTo(b);
c = b.CompareTo(a);
-1 (This means a is smaller than b)
1 (This means b is smaller than a)
Notes, globalization. By default, string.Compare and CompareTo use the system globalization for safe comparisons. Cultures such as Turkish have common letters that need globalization.
Tip: You can specify those cultures in the method call as a parameter to accomplish this goal.
A discussion. All sorting algorithms must see if one string should be ordered before any other string. The Compare methods are ideal for when you need to implement your own sorting methods.
Note: Compare methods are not needed to check string equality. You can use the Equals method, or the == operator, for equality.