1. Member
Join Date
Aug 2013
Location
Munich, Germany
Posts
3
Rep Power
0

## Card Sort Algorithm

Hi,

I am trying to write a program that is sorting a hand, containing 8 cards.
Each card has a color and a value. The program should compare all 8 cards and start with the highest value. If 2 or more cards have the same value the color will decide the priority.
I made a research on what possibilities I have in Java and found the compareTo-Solution, but it is not working, so maybe somebody can help me or give me the advise I need to complete my program.

public int compareTo(Karte o) {

if(this.getScore()<o.getScore())
return -1;
else if(this.getScore()==o.getScore())
return 0;
else
return 1;

}
My idea was to give the method a cardList, then to save this list in a temporary list, clear the cardList and then fill it up again with the cards from the temporary list card by card. But I still dont understand how this CompareTo really works and how to get the next object in the list.

public List<Karte> CardsSort(List<Card> cardList) {
List<Karte> temp = new ArrayList<Karte>();
for (Iterator iter = cardList.iterator(); iter.hasNext();) {
Karte karte = (Karte) iter.next();
}
cardList.clear();

if(card.compareTo(next)>0)
else if(card.compareTo(next)==0)
{
if(next.getColor()!=Color.HERZ)
else if(next.getColor()==Color.HERZ)
else if(card.getScore()>next.getScore())
else
}
else
{ if(card.getColor()!=Color.HERZ)
}
return cardList;
}

I would be very happy if someone could help me in this issue.

Andrej

2. ## Re: Card Sort Algorithm

Hi Andrej,

I'd implement a Comparator, using the values and colors, then I'd deal the hand unsorted and last call Collections.sort(List, Comparator).
There's no need to implement the sorting yourself.

Phil

3. ## Re: Card Sort Algorithm

Guide For New Members
BB Code List - Java Programming Forum - Learn Java Programming

db

4. Member
Join Date
Aug 2013
Location
Cuxhaven, Germany
Posts
5
Rep Power
0

## Re: Card Sort Algorithm

If you implemented the class "Karte" change it to

Java Code:
```public class Karte implements Comparable<Karte> {
...
}```
Then you have to implement the method

Java Code:
```@Override
public int compareTo(Karte k) {
return this.score.compareTo(k.score);
}```
if type of score (i think it is int or double) implements Comparable (int and double do), or

Java Code:
```@Override
public int compareTo(Karte k) {
return (this.score < k.score ? -1 : (this.score == k.score ? 0 : 1));
}```
if type of score does not.

[ return (a == b ? 1 : 0); is the same as if(a==b) return 1; else return 0; ]

Then you just call

Java Code:
`Collections.sort(cardList);`
after you inserted the cards.

Regards,

raz0r

EDIT: Oh, its not int and double, but Integer and Double that implement Comparable...

You could cast the score or use the second version of the method.

compareTo with cast:

Java Code:
```@Override
public int compareTo(Karte k) {
return ((Integer)this.score).compareTo((Integer)k.score);
}```
EDIT2: Oh and i missed the card colors ...

declare the colors in the right order:

Java Code:
```public enum Color {
KARO,
HERZ,
PIK,
KREUZ;

...
}```
then you just change your compareTo method to

Java Code:
```@Override
public int compareTo(Karte k) {
int c = ((Integer)this.score).compareTo((Integer)k.score);
return (c != 0 ? c : (this.color.compareTo(k.color)));
}```
because all enums implement Comparable.
Last edited by raz0r; 08-27-2013 at 03:50 PM.

5. ## Re: Card Sort Algorithm

I don't see any advanced stuff in here so I moved the thread.

kind regards,

Jos

6. Member
Join Date
Aug 2013
Location
Munich, Germany
Posts
3
Rep Power
0

## Re: Card Sort Algorithm

Originally Posted by PhHein
Hi Andrej,

I'd implement a Comparator, using the values and colors, then I'd deal the hand unsorted and last call Collections.sort(List, Comparator).
There's no need to implement the sorting yourself.

Phil
Many, many thanks@all. I have something that looks very similar to your answer raz0r.
I still got strange errors, but I am confident to fix it.
Yes maybe advanced was not best area.
Andrej

7. Member
Join Date
Aug 2013
Location
Munich, Germany
Posts
3
Rep Power
0

## Re: Card Sort Algorithm

Originally Posted by Starfall
Many, many thanks@all. I have something that looks very similar to your answer raz0r.
I still got strange errors, but I am confident to fix it.
Yes maybe advanced was not best area.
Andrej
Hey again,
I moved forward but still is not the result is was hoping for.
I want the cards not only sorted by value and and grouped by color but also there is the difference between trumps and not-trumps.
For instance. The "Unters"(4) and "Obers"(4) + all hearts are trumps so there are 14 at all. I want it sorted first by trumpf then by value grouped by color.

Right now in my main I have:
public static void main(String[] args) {

TreeSet<Karte> handDesSpielers = new TreeSet<Karte>();

for(Card card:handDesSpielers)
{

System.out.println(card.getColor() +" "+ card.getWert());
}
}
and the sorted result is:

HERZ ACHT
EICHEL OBER
EICHEL UNTER
EICHEL ZEHN
EICHEL ACHT
GRAS OBER
GRAS UNTER
SCHELLEN SAU

but should be:
EICHEL OBER
GRAS OBER
EICHEL UNTER
GRAS UNTER
HERZ ACHT
EICHEL ZEHN
EICHEL ACHT
SCHELLEN SAU

My compareTo() in the Class Card:
public int compareTo(Karte o) {

int c = (this.getColor()).compareTo(o.getColor());
return (c != 0 ? c : (this.getPriority() - ((o.getPriority()))));
}
+ Constructor
public Card(Color color,Wert wert) {
this.color = color;
this.setWert(wert);
switch (wert) {
case SAU: this.score= 11; this.priority = 3;break;
case ZEHN: this.score = 10; this.priority = 4;break;
case KOENIG: this.score = 4; this.priority = 5;break;
case OBER: this.score = 3; this.priority = 1;break;
case UNTER: this.score = 2; this.priority = 2;break;
case NEUN: this.score = 0; this.priority = 6;break;
case ACHT: this.score = -1; this.priority = 7;break;
case SIEBEN: this.score = -2; this.priority = 8;break;
}
}

Am happy for any hint.
Andrej

8. ## Re: Card Sort Algorithm

Starfall, I gave you two links at #3. I expect you to go through them and edit your posts.

Failure to comply may result in this thread being closed. Repeated failure may result in a ban.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•