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 970675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:50:34+00:00 2026-05-16T02:50:34+00:00

I am working on a program (learning C#) that creates a object (Card) and

  • 0

I am working on a program (learning C#) that creates a object (Card) and in the program it sorts the deck of cards by value. What I am baffled by is how it is able to sort a deck of cards by the value of the cards when the instance of the object isn’t the parameter passed to the method but the class object. Below is the code to help answer my question – I would complete follow this if I was passing the instantiated object.

The class to create the object:

class Card
{
    public Suits Suit { get; set; }
    public Values Value { get; set; }

    public Card(Suits suit, Values value)
    {
        this.Suit = suit;
        this.Value = value;
    }

    public string Name
    {
        get { return Value.ToString() + " of " + Suit.ToString(); }
    }

    public override string ToString()
    {
        return Name;
    }
}

In another class I am calling the method that instantiate a object(CardComparer_byValue) that inherits from interface IComparer. This is where my confusion comes in.

public void SortByValue() {
cards.Sort(new CardComparer_byValue());
} 

And the SortByValue Class

class CardComparer_byValue : IComparer<Card>
{
    public int Compare(Card x, Card y)
    {
        if (x.Value > y.Value)
            return 1;
        if (x.Value < y.Value)
            return -1;
        if (x.Suit > y.Suit)
            return 1;
        if (x.Suit < y.Suit)
            return -1;
        return 0;
    }

} 

I would think the above method “SortByValue” was called like this

Card mycard = new Card("Spades", 4);
public void SortByValue() {
cards.Sort(new CardComparer_byValue(mycard));
} 

So would someone explain to me what I am missing on how the sort can work this way?

  • 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-05-16T02:50:34+00:00Added an answer on May 16, 2026 at 2:50 am

    It’s sorting the entire collection, not against a specific “card”.

    That’s why the Compare method is defined like:

    public int Compare(Card x, Card y)
    

    It uses “Card x” and “Card y”, calling this on pairs of elements within the cards collection, in order to sort it. The Sort method calls this Compare method many times, using it to sort the entire collection by comparing elements in pairs one pair at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working program in C++ that generates data for a Mandelbrot Set.
I'm learning Ruby and working on my first real project. The program will receive
I've been working at learning programming for a while now and I feel that
I am working on a POSIX C learning exercise that involves recursively listing files/folders
I am working on a learning program and using the Code Rush refactoring tool
I'm currently working on a small utility program that only requires a command line
I'm currently working with Java to write a program that does an EAI between
I am working on a program that needs to do the following: Write a
is there any small working program for recieving from and sending data to client
I have a working C program where the length of an array of strings

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.