Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-13-2010, 03:53 AM   PM User | #1
ADAMWD
New to the CF scene

 
Join Date: Nov 2010
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
ADAMWD is an unknown quantity at this point
Sorting an array of objects

I'm working on a card game, and I need to sort an array that contains card objects. The array needs to be sorted based on the numerical value of each card in ascending order. I have a method, getValue, that returns the value of the card at the element that the method is called upon.

But, I am unsure of how exactly to sort the data correctly, as I am not too familiar with sorting in Java. Any help would be great.

EDIT: Actually, I just figured it out on my own. I just created a new array that contained just the numerical values of the array of cards. Then I just simply sorted that array of ints, since in this case I only cared about the value of the cards, not the suit.

Last edited by ADAMWD; 11-13-2010 at 04:05 AM..
ADAMWD is offline   Reply With Quote
Old 11-13-2010, 03:56 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
The multiple array juggling will work, but it will become more complicated as you get more and more dimensions.
Sorting objects in Java is very easy. A flat array uses the static Arrays.sort method in combination with a Comparator<T> or a class of type Comparable<T>. So with your Card class:
PHP Code:
public class Card implements Comparable<Card>
{
    
// Whatever card stuffs
    
public int compareTo(Card b)
    {
        return 
a.getValue() - b.getValue(); // These can be just the members
    
}

    public 
boolean equals(Object other)
    {
        
boolean bResult false;
        if (
other instanceof Card)
        {
            
bResult = (this.compareTo((Card)other) == 0);
        }
        return 
bResult;
    }

Then in a main method, my card array will be called cards:
Code:
Arrays.sort(cards);
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
ADAMWD (11-13-2010)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:31 AM.


Advertisement
Log in to turn off these ads.