Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Making item sortable (implementing Comparable)

  1. #1
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Making item sortable (implementing Comparable)

    Hello,

    The problem is in this part of the code:
    CardBase[] tmp = new CardBase[13];
    System.arraycopy(deck, range*13, tmp, 0, 13);
    Collections.sort(Arrays.asList(tmp));
    The error I get is:
    cannot find symbol
    symbol  : method sort(java.util.List<CardBase>)
    location: class java.util.Collections
                    Collections.sort(Arrays.asList(tmp));

    CardBase is an interface which looks like this:
    public interface CardBase {
    	public int compareTo(Object o);
    	public Suit setSuit();
    	public Suit getSuit();
    	public String getShortSuit();
    }

    The deck array which is being copied with System.arraycopy method to a tmp array is:
    CardBase[ ] deck = new Card[52];

    I know that to be able to sort items, I need to make it sortable first. Card class is the class which implements CardBase and Comparable interfaces. So from my point of view everything should be ok, but it isn't. Anyone could tell me where the mistake is? If it is not enough information, I can paste full code here.

    P.S
    compareTo method is ok, but I'll post it anyway:
    public int compareTo(Object o) {
    		if(o instanceof Card) {
    			Card tmp = (Card) o;
    			int i = this.suit.compareTo(tmp.getSuit());
    			if(i == 0)
    				return this.label.compareTo(tmp.getLabel());
    			else
    				return i;
    		}
    		return -1;
    	}
    Last edited by Asido; August 19th, 2010 at 11:58 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Making item sortable (implementing Comparable)

    From the point of view of the sorting algorithm, CardBase isn't sortable even though it's actually holding Cards (simply having the CompareTo method declared inside of CardBase doesn't make it Comparable).

    Consider this:

    If you had a second class implement CardBase (say Card2) that wasn't Comparable, you could still add it to the array tmp. The sorting algorithm would then puke because it can't sort items of type Card2.

    The simple fix is rather than having your Card class directly implement the Comparable interface, have the CardBase interface extend Comparable so you guarantee that all classes that implement CardBase will be Comparable.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Asido (August 19th, 2010)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,575
    Thanks
    45
    Thanked 1,954 Times in 1,928 Posts

    Default Re: Making item sortable (implementing Comparable)

    Don't have a solution.
    The API doc for sort says:
    All elements in the list must implement the Comparable interface.
    I don't see where you honor this requirement

  5. #4
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Making item sortable (implementing Comparable)

    Quote Originally Posted by Norm View Post
    Don't have a solution.
    The API doc for sort says:
    All elements in the list must implement the Comparable interface.
    I don't see where you honor this requirement
    Just read what I wrote once more
    TIP:
    Card class is the class which...
    Anyway, with helloworld922 advice I have solved it. Thanks!

Similar Threads

  1. drag item or insert item into new Jlabel in JPanel
    By qaromi in forum AWT / Java Swing
    Replies: 5
    Last Post: July 6th, 2010, 07:37 PM
  2. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  3. Comparable Interface
    By yelrubk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:08 AM
  4. [SOLVED] how to delete an item from a form
    By mahdi in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 30th, 2009, 01:06 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM