
Compare determines the sort order of strings. It checks if one string is ordered before another when in alphabetical order, whether it is ordered after, or is equivalent. The .NET Framework's comparison methods, including Compare, CompareOrdinal and CompareTo provide this functionality for the C# language. They are well-tested.
String Compare method results String A: First alphabetically String B: Second alphabetically Compare(A, B): -1 Compare(B, A): 1 Compare(A, A): 0 Compare(B, B): 0

Here we look at how you can use the Compare instance methods and related methods on the string type. Normally you need the Compare methods for sorting algorithms and testing whether one string comes before or after the other in a plain ASCII sort.
This C# program uses the string.Compare and string.CompareOrdinal methods.
Program that uses Compare [C#]
using System;
class Program
{
static void Main()
{
string a = "a"; // 1
string b = "b"; // 2
int c = string.Compare(a, b);
Console.WriteLine(c);
c = string.CompareOrdinal(b, a);
Console.WriteLine(c);
c = a.CompareTo(b);
Console.WriteLine(c);
c = b.CompareTo(a);
Console.WriteLine(c);
}
}
Output
-1 (This means a is smaller than b)
1 (This means b is smaller than a)
-1
1Description. The program shows three comparison methods on the string type. The Compare and CompareOrdinal methods are static on the String type, while the CompareTo method is an instance method on the variable itself. If the first string is bigger, the result is 1. If both strings are equal, the result is 0. If the first string is smaller, the result is -1.
Understanding globalization. One important advantage of these comparison methods is that they can be customized to use any globalization context you require. By default, the string.Compare and CompareTo methods use the system globalization for safe comparisons. Cultures such as Turkish have common letters that need globalization. You can specify those cultures in the method call as a parameter to accomplish this goal.

Because 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. The Compare methods are not useful when you just want to check one string for equivalence to another string. You can use the Equals method, or the == operator, for simpler code in that case.
String Equals Examples
String comparisons in the .NET Framework are by default case-sensitive, meaning that "A" is considered different from "a". However, you can use the StringComparison.OrdinalIgnoreCase enumerated constant to use a case-insensitive comparison. This option is useful in many real-world programs, such as those that deal with the Windows file system where file case is not important.

You can use the result of the Compare methods in the C# programming language to see if one string is larger, smaller, or equal to another string. The method will return 1 if the first string is alphabetically first, 0 if the two strings are equal, and -1 if the first string is alphabetically second. Finally, we noted where the Compare methods are best used and how you can modify their behavior with the StringComparison enumerated type.
StringComparison Enum String Type