C# StringComparer.Compare() Method
In C#, the Compare method is used to compare two objects or strings and returns an indication of their relative sort order.
Example: C# program to illustrate how to use StringComparer() to perform ordinal comparison.
// C# program to illustrate the
// CompareOrdinal(String, Int32,
// String, Int32, Int32) method
using System;
class Geeks
{
static public void Main()
{
string s1 = "GFG";
string s2 = "GFG";
int res = StringComparer.Ordinal.Compare(s1, s2);
if (res < 0)
Console.WriteLine($"{s1} is less than {s2}");
else if (res > 0)
Console.WriteLine($"{s1} is greater than {s2}");
else
Console.WriteLine($"{s1} is equal to {s2}");
}
}
// C# program to illustrate the
// CompareOrdinal(String, Int32,
// String, Int32, Int32) method
using System;
class Geeks
{
static public void Main()
{
string s1 = "GFG";
string s2 = "GFG";
int res = StringComparer.Ordinal.Compare(s1, s2);
if (res < 0)
Console.WriteLine($"{s1} is less than {s2}");
else if (res > 0)
Console.WriteLine($"{s1} is greater than {s2}");
else
Console.WriteLine($"{s1} is equal to {s2}");
}
}
Output
GFG is equal to GFG
Explanation: In this example, the StringComparer.Ordinal.Compare() method checks if two strings are equal. It returns 0 when both strings are identical, a negative value if the first string is less, and a positive value if it is greater.
Overloads of StringComparer.Compare() Method
There are 2 methods in the overload list of this method:
1. Compare(Object a, Object b)
This method compares two objects and returns an indication of their relative sort order when overridden in a derived class.
Syntax:
public int Compare (object a, object b);
- Parameters: This method takes two parameter, object a is the first object which has to be compared and the object b is the second object which has to be compared with the object a.
- Return Type: This method returns 0, if both the objects are equal also return positive number if object a is lexicographically greater than object b, also return negative number if the object a is lexicographically lesser than the object b.
Note: This method throws an ArgumentException if the objects being compared are not of the same type.
Example: In this example, we are comparing two objects using the StringComparer.Compare(Object, Object) method.
// Demonstrating the working of
// StringComparer.Compare() method
using System;
class Geeks
{
static void Main()
{
// Creating two objects holding string
// values "Geek" and "Geeks"
object obj1 = "Geek";
object obj2 = "Geeks";
// Using StringComparer.Ordinal
// to compare the two string objects
int result = StringComparer.Ordinal.Compare(obj1, obj2);
// Checking the result of comparison and
// printing appropriate message
if (result < 0)
Console.WriteLine($"{obj1} is less than {obj2}");
else if (result > 0)
Console.WriteLine($"{obj1} is greater than {obj2}");
else
Console.WriteLine($"{obj1} is equal to {obj2}");
}
}
// Demonstrating the working of
// StringComparer.Compare() method
using System;
class Geeks
{
static void Main()
{
// Creating two objects holding string
// values "Geek" and "Geeks"
object obj1 = "Geek";
object obj2 = "Geeks";
// Using StringComparer.Ordinal
// to compare the two string objects
int result = StringComparer.Ordinal.Compare(obj1, obj2);
// Checking the result of comparison and
// printing appropriate message
if (result < 0)
Console.WriteLine($"{obj1} is less than {obj2}");
else if (result > 0)
Console.WriteLine($"{obj1} is greater than {obj2}");
else
Console.WriteLine($"{obj1} is equal to {obj2}");
}
}
Output
Geek is less than Geeks
2. Compare(String a, String b)
This method compares two strings and returns an indication of their relative sort order when overridden in a derived class.
Syntax:
public abstract int Compare (string a, string b);
- Parameters: This method takes two paramter, string a is the first string which has to be compared and the string b is the second string which has to be compared with the string a.
- Return Type: This method returns zero if both the string objects are equal, it also returns a positive number if the string a is greater than string b, it also returns a negative number if the string a is lesser than the string b.
Note: This method throws an ArgumentException
Example: In this example, we are comparing two string objects using the StringComparer.Compare(String, String) method.
// Demonstrating the working of
// Compare(String a, String b) method
using System;
class Geeks
{
static void Main()
{
// Define two strings to compare
string s1 = "geek";
string s2 = "Geek";
// Compare the two strings using
// ordinal (Unicode) comparison
int result = StringComparer.Ordinal.Compare(s1, s2);
// Print the comparison result
// Positive number means s1 > s2,
// negative means s1 < s2,
// zero means equal
Console.WriteLine(result);
}
}
// Demonstrating the working of
// Compare(String a, String b) method
using System;
class Geeks
{
static void Main()
{
// Define two strings to compare
string s1 = "geek";
string s2 = "Geek";
// Compare the two strings using
// ordinal (Unicode) comparison
int result = StringComparer.Ordinal.Compare(s1, s2);
// Print the comparison result
// Positive number means s1 > s2,
// negative means s1 < s2,
// zero means equal
Console.WriteLine(result);
}
}
Output
32