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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:56:07+00:00 2026-05-25T02:56:07+00:00

I am creating a generic class that has a method that is supposed to

  • 0

I am creating a generic class that has a method that is supposed to sort through an array collection that may either be a Book or it may be a CD. I implemented Comparable in the two type classes and am trying to sort the array in the GenericCollection class.

My method looks like this right now:

public void sort(){
    for (int i = 0; i < collection.length; i++) {
        for (int j = 0; j < collection.length; j++) {
            if (((T)collection[i]).compareTo((T)collection[j]) > 0){
                T t = (T)collection[i];
                collection[i] = collection[j];
                collection[j] = t;
            }                   
        }
    }
}

This gives me the error that Object is not comparable. If I try to cast to type T then I get the error that T is undefined for CompareTo. If I try something like:

<T extends Comparable>

I can compile but I get a runtime error even though I have Comparable defined in both types that are using this class.

How do I get the Generic type to be able to run the compareTo methods in their own classes.

Thanks for any help.

  • 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-25T02:56:08+00:00Added an answer on May 25, 2026 at 2:56 am

    If you need to implement your own sort algorithm, try to declare the type parameter of your generic collection class as

     <T extends Comparable<T>>
    

    This means, your class would be declared like this:

     class GenericCollection<T extends Comparable<T>> {  ... }
    

    And of course, your book or CD classes should implement this exact type:

     class Book implements Comparable<Book> { ... }
    
     class CD implements Comparable<CD> { ... }
    

    If you use the parameter <T extends Comparable>, you are using a raw type, and these are only for compatibility with old (pre-generics) code, should never be used in new code.

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

Sidebar

Related Questions

I'm creating a generic list class that has a member of type Array(Array of
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
I'm creating a base class that has an isDirty flag. It is set any
I am creating a really basic program, that has a method to fill an
So here's what I'm trying to do. I'm creating a generic class that allocates
This is best explained using code. I have a generic class that has a
I'm creating a generic class and in one of the methods I need to
I am creating a generic error handling / logging class for our applications. The
C#, .net 3.5 I am trying to create a base class that has a
I'm creating a tree structure that is based on an AbstractNode class. The AbstractNode

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.