C# | StringComparer.Compare Method

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)

Compare(Object, Object)

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:

Value Meaning
Less than zero a precedes b in the sort order or a is null and b is not null.
Zero a is equal to b or a and b are both null.
Greater than zero a follows b in the sort order or b is null and a is not null.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the use of
// StringComparer.Compare(Object, Object)
// Method
using 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();
    }
}

chevron_right


Output:

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

Compare(String, String)

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:

Value Meaning
Less than zero a precedes b in the sort order or a is null and b is not null.
Zero a is equal to b or a and b are both null.
Greater than zero a follows b in the sort order or b is null and a is not null.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the use of 
// StringComparer.Compare(String, String)
// Method
using 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());
    }
}

chevron_right


Output:

-1

Reference:



My Personal Notes arrow_drop_up

3rd year student of Information Technology JADAVPUR UNIVERSITY

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.