C# StringComparer.Compare() Method
Last Updated :
19 May, 2025
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#
// 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}");
}
}
OutputGFG 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.
C#
// 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}");
}
}
OutputGeek 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.
C#
// 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);
}
}
Similar Reads
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#// C# program to illustrate the // CompareOrdinal(String, Int32, // String, Int32,
3 min read
C# String CompareOrdinal() Method In C#, CompareOrdinal() is a method of the String class. This method is used to compare the two specified string objects or substrings using the numerical values (Unicode) of the corresponding Char objects in each string or substring. It performs a case-sensitive and culture-insensitive comparison.O
4 min read
How to Compare Strings in C#? A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s
13 min read
C# | String.Contains() Method In C#, the String.Contains() method is used to check whether a substring exists within a given string. It returns a Boolean value (true or false) indicating whether the substring is found. By default, the method performs a case-sensitive comparison.Example 1: Here, we are using the String.Contains()
3 min read
C# String EndsWith() Method In C#, the EndsWith() is a string method used to check whether the ending of the current string instance matches a specified string. If it matches, it returns true; otherwise, it returns false. Using the foreach loop, we can check multiple strings. This method supports overloading by passing differe
4 min read
C# | Equals(String, String) Method In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false. This method is different from Compare and CompareTo method
2 min read