CSharp - Using custom comparer with OrderBy

Introduction

The IComparer<T> Interface is as follows:

interface IComparer<T> {
       int Compare(T x, T y);
}

IComparer interface requires us to implement a single method named Compare.

This method will accept two arguments of the same type T and will return an int that is

  • less than zero if the first argument is less than the second,
  • zero if the two arguments are equal, and
  • greater than zero if the second argument is greater than the first.

The following code created a class that implements the IComparer interface, which will order the elements based on their vowel-to-consonant ratios.

public class MyVowelToConsonantRatioComparer : IComparer<string>
{
    public int Compare(string s1, string s2)
    {
        int vCount1 = 0;
        int cCount1 = 0;
        int vCount2 = 0;
        int cCount2 = 0;

        GetVowelConsonantCount(s1, ref vCount1, ref cCount1);
        GetVowelConsonantCount(s2, ref vCount2, ref cCount2);

        double dRatio1 = (double)vCount1 / (double)cCount1;
        double dRatio2 = (double)vCount2 / (double)cCount2;

        if (dRatio1 < dRatio2)
            return (-1);
        else if (dRatio1 > dRatio2)
            return (1);
        else
            return (0);
    }

    public void GetVowelConsonantCount(string s,
                                       ref int vowelCount,
                                       ref int consonantCount)
    {

        string vowels = "AEIOU";
        vowelCount = 0;
        consonantCount = 0;
        string sUpper = s.ToUpper();

        foreach (char ch in sUpper)
        {
            if (vowels.IndexOf(ch) < 0)
                consonantCount++;
            else
                vowelCount++;
        }

        return;
    }
}

The class above contains two methods, Compare and GetVowelConsonantCount.

The Compare method is required by the IComparer interface.

The GetVowelConsonantCount method returns the number of vowels and consonants for a given input string.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
class Program//from w w w . j a va 2  s.c  om
{
    static void Main(string[] args)
    {
        string[] codeNames = {
           "Python", "Java", "Javascript", "Bash", "C++", "Oracle"};

        MyVowelToConsonantRatioComparer myComp = new MyVowelToConsonantRatioComparer();

        IEnumerable<string> namesByVToCRatio = codeNames
          .OrderBy((s => s), myComp);

        foreach (string item in namesByVToCRatio)
        {
            int vCount = 0;
            int cCount = 0;

            myComp.GetVowelConsonantCount(item, ref vCount, ref cCount);
            double dRatio = (double)vCount / (double)cCount;

            Console.WriteLine(item + " - " + dRatio + " - " + vCount + ":" + cCount);
        }
    }
}
public class MyVowelToConsonantRatioComparer : IComparer<string>
{
    public int Compare(string s1, string s2)
    {
        int vCount1 = 0;
        int cCount1 = 0;
        int vCount2 = 0;
        int cCount2 = 0;

        GetVowelConsonantCount(s1, ref vCount1, ref cCount1);
        GetVowelConsonantCount(s2, ref vCount2, ref cCount2);

        double dRatio1 = (double)vCount1 / (double)cCount1;
        double dRatio2 = (double)vCount2 / (double)cCount2;

        if (dRatio1 < dRatio2)
            return (-1);
        else if (dRatio1 > dRatio2)
            return (1);
        else
            return (0);
    }

    public void GetVowelConsonantCount(string s,
                                       ref int vowelCount,
                                       ref int consonantCount)
    {

        string vowels = "AEIOU";
        vowelCount = 0;
        consonantCount = 0;
        string sUpper = s.ToUpper();

        foreach (char ch in sUpper)
        {
            if (vowels.IndexOf(ch) < 0)
                consonantCount++;
            else
                vowelCount++;
        }

        return;
    }
}

Result

Related Topic