# Card Sort Algorithm

Printable View

• 08-27-2013, 12:31 PM
Starfall
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();
temp.add(karte);
}
cardList.clear();

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

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

Andrej
• 08-27-2013, 12:41 PM
PhHein
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
• 08-27-2013, 02:24 PM
DarrylBurke
Re: Card Sort Algorithm
• 08-27-2013, 02:33 PM
raz0r
Re: Card Sort Algorithm
If you implemented the class "Karte" change it to

Code:

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

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

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

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:

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:

Code:

```public enum Color {     KARO,     HERZ,     PIK,     KREUZ;     ... }```
then you just change your compareTo method to

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.
• 08-27-2013, 04:42 PM
JosAH
Re: Card Sort Algorithm
I don't see any advanced stuff in here so I moved the thread.

kind regards,

Jos
• 08-28-2013, 12:21 AM
Starfall
Re: Card Sort Algorithm
Quote:

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.:P:
Andrej
• 09-01-2013, 02:41 PM
Starfall
Re: Card Sort Algorithm
Quote:

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.:P:
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>();
cardHand.add(new Card(Color.HERZ,Wert.ACHT));
cardHand.add(new Card(Color.SCHELLEN,Wert.SAU));
cardHand.add(new Card(Color.GRAS,Wert.UNTER));
cardHand.add(new Card(Color.GRAS,Wert.OBER));
cardHand.add(new Card(Color.EICHEL,Wert.ZEHN));
cardHand.add(new Card(Color.EICHEL,Wert.OBER));
cardHand.add(new Card(Color.EICHEL,Wert.UNTER));
cardHand.add(new Card(Color.EICHEL,Wert.ACHT));

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
• 09-01-2013, 04:51 PM
DarrylBurke
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