C# | StringComparer.Compare Method - GeeksforGeeks

30 Jun, 2019
StringComparer.Compare Method is used to compare two objects or strings and returns an indication of their relative sort order. There are 2 methods in the overload list of this method:
Compare(Object, Object)
Compare(String, String)
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);
Here, a is the 1st object and b is the 2nd object to be compared with each other.
Returns: This method returns a signed integer that indicates the relative values of the object a and b. The values are returned according to the following table:
Exception: This method will give ArgumentException if neither a nor b is a String object, and neither a nor b implements the IComparable interface.
Example:
// C# program to demonstrate the use of// StringComparer.Compare(Object, Object)// Methodusing System;using System.Collections;  class gfg {      public class cmp : IComparer {          // CaseInsensitiveComparer        int IComparer.Compare(Object x, Object y)        {            return ((new CaseInsensitiveComparer()).Compare(x, y));        }    }      // Main Method    public static void Main()    {        // Initialize a string array.        string[] arr = {"A", "E", "D", "C", "B"};          // Display original array        Console.WriteLine("Original array:");        print(arr);          // Sort the array using the default comparer.        Array.Sort(arr);        Console.WriteLine("Sort using sort function:");        print(arr);          // Sort the array using the comparer.        Array.Sort(arr, new cmp());        Console.WriteLine("Sorting using compare method :");        print(arr);    }      // print function    public static void print(IEnumerable list)    {        foreach(var v in list)            Console.WriteLine(v);          Console.WriteLine();    }}
Original array:
A
E
D
C
B

Sort using sort function:
A
B
C
D
E

Sorting using compare method :
A
B
C
D
E

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);
Here, a is the 1st string and b is the 2nd string to be compared with each other.
Returns: This method returns a signed integer that indicates the relative values of the object a and b. The values are returned according to the following table:
Exception: This method will give ArgumentException if neither a nor b is a String object, and neither a nor b implements the IComparable interface.
Example:
// C# program to demonstrate the use of // StringComparer.Compare(String, String)// Methodusing System;using System.Collections.Generic;  class GFG {      // Main Method    static void Main(string[] args)    {        string s1 = "geek";        string s2 = "Geek";          int st = 0;          // Compare(string, string) method        st = string.Compare(s1, s2);          Console.WriteLine(st.ToString());    }}
-1

Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.stringcomparer.compare?view=netframework-4.8
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
C# | Inheritance
C# | Generics - Introduction
Top 50 C# Interview Questions & Answers
Switch Statement in C#
Convert String to Character Array in C#
C# | How to insert an element in an Array?
Lambda Expressions in C#