Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8166209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:46:54+00:00 2026-06-06T19:46:54+00:00

class Program { public class Pokemon { public string Name; public int Weight; }

  • 0
class Program
{
    public class Pokemon
    {
        public string Name;
        public int Weight;
    }


    public class myReverserClass : IComparer
    {

        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object x, Object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(x, y));
        }
    }


    static void Main(string[] args)
    {
        IComparer myComparer = new myReverserClass();

        Random r = new Random();

        string[] pokemons = new string[10];
        pokemons[0] = "Bulbasaur";
        pokemons[1] = "Charmander";
        pokemons[2] = "Squirtle";
        pokemons[3] = "Groulit";
        pokemons[4] = "Caterpie";
        pokemons[5] = "Weedle";
        pokemons[6] = "Kakuna";
        pokemons[7] = "Beedrill";
        pokemons[8] = "Pidgey";
        pokemons[9] = "Rattata";

        string[] sovpad = new string[10];
        Pokemon[] pokemon = new Pokemon[10];

        for (int i = 0; i < pokemons.Length; i++)
        {
            string sovpadtrue;
            sovpadtrue = pokemons[r.Next(0, pokemons.Length)];
            for(int p = 0; p<sovpad.Length;p++)
            {
                if(sovpadtrue == sovpad[p])
                {
                    sovpadtrue = pokemons[r.Next(0, pokemons.Length)];
                    p = -1;
                }
            }
            sovpad[i] = sovpadtrue;
            pokemon[i] = new Pokemon();
            pokemon[i].Name = sovpadtrue;
            pokemon[i].Weight = r.Next(10, 1000);
        }
        Array.Sort(pokemon,myComparer);

        Console.ReadLine();
    }
}

How to add a class myReverserClass, to sort an array of pokemon, first by name, and then the weight?
Error: In at least one object must implement the interface IComparable.
On Msdn example of one-dimensional array only. And myReverserClass taken from msdn.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:46:56+00:00Added an answer on June 6, 2026 at 7:46 pm

    CaseInsensitiveComparer.Compare documentation includes this:

    If a and b are both strings, this method uses CompareInfo.Compare to compare the strings with the casing ignored; otherwise, it uses the IComparable implementation of either object. That is, if a implements IComparable, then this method returns the result of a. CompareTo (b); otherwise, if b implements IComparable, then it returns the negated result of b. CompareTo (a).

    and in exceptions:

    ArgumentException: Neither a nor b implements the IComparable interface.

    In this case, you’re comparing two Pokemon objects, and Pokemon doesn’t implement IComparable – hence the exception.

    You’d be much better off using the generic comparisons, and writing a class which implements IComparer<Pokemon>. For example:

    public class PokemonReverseNameComparer : IComparer<Pokemon>
    {
        public int Compare(Pokemon x, Pokemon y)
        {
            return StringComparer.OrdinalIgnoreCase.Compare(y.Name, x.Name);
        }
    }
    

    Note that here I have actually reversed the argument order – whereas you claimed to do so in the comment, but still actually called Compare(x, y). (This doesn’t do the weight part though – it’s important that you understand one part at a time…)

    Personally I’d approach things rather differently, separating out the reversal from the “compare by name” part, and I’d also try to use LINQ, but that’s a different matter. LINQ makes it really easy – for example:

    var ordered = pokemon.OrderByDescending(p => p.Name)
                         .ThenBy(p => p.Weight);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { public delegate void VoidMethodDelegate(); public delegate int IntMethodDelegate(); static void Main(string[]
Suppose public class Program { public static void Main(string[] args) { Program p =
class Foo { public int A { get; set; } } class Program {
If I run the following program: class Runit{ public static void main(String[] argsWut) throws
I am compiling a simple program class Test { public static void main(String[] args)
Why is this code not working? public class A { public Dictionary<int, string> dic
using System; public delegate void Printer(string s); class Program { public static void Main(string[]
I have a Enumeration as shown in below program public class Test { public
First, here is the C# code and the disassembled IL: public class Program<T> {
Consider the following program: class A { public static void Foo() { } }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.