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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:08:14+00:00 2026-05-28T17:08:14+00:00

I am implementing a generic PriorityQueue in C# as part of homework. The items

  • 0

I am implementing a generic PriorityQueue in C# as part of homework.
The items are stored in an array.

class PQueue<T> : IPQueue<T>
{
    T[] items;
    //..
}

How can I compare two items. I guess the type with which PQueue is instantiated has to implement IComparable/IComparer. If so, how can I compare two elements in items?

What is the elegant way to design this.

  • 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-28T17:08:15+00:00Added an answer on May 28, 2026 at 5:08 pm

    First, you need to tell C# that <T> implements IComparable<T>

    class PQueue<T> : IPQueue<T> where T : IComparable<T> {
        T[] items;
        //..
    }
    

    Now you can compare individual items, like this:

    var cmp = items[i].CompareTo(items[j]);
    if (cmp < 0) {
        // items[i] is less than items[j]
    } else if (cmp > 0) {
        // items[i] is greater than items[j]
    } else {
        // Items are equal
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Fixed: See notes at bottom I am implementing a generic class that supports two
I am implementing a generic Priority Queue as part of a homework project. I
If you're implementing generic extension method for generic class is there a better way?
Given the following generic interface and implementing class: public interface IRepository<T> { // U
I have a generic class implementing IValueConverter . Something like: class MyValueConverter<T> : IValueConverter
I would like to have a generic Interpolator class which can interpolate between instances
I've got two generic base classes. The second generic class has a constraint on
I'm implementing generic, persistent .NET collections based on top of the ESENT database engine
A couple of days ago, I read a blog entry ( http://ayende.com/Blog/archive/2008/09/08/Implementing-generic-natural-language-DSL.aspx ) where
i'm implementing a generic interface (iqueryprovider, specifically). at some point, i'm forced to return

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.