0

I have an array that holds the ranks of playing cards: A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K. (The "T" represents the 10).

I have all of these stored in an ArrayList.

For example, I have an ArrayList that represents the clubs and has all of the card ranks in it:

ArrayList<Character> clubs = new ArrayList<Character>();

If I print all of the elements in this ArrayList, they print out in the following order:

3 2 4 5 6 8 7 9 T J Q K A 

I then added this to sort the collection:

Collections.sort(clubs);

With the collection sort, it prints the elements in this order:

2 3 4 5 6 7 8 9 A J K Q T

How can I make it so that it prints in this order: ?

A 2 3 4 5 6 7 8 9 T J Q K
5
  • 1
    You can look into this thejavageek.com/2013/06/17/sorting-user-defined-objects-part-2 Commented Sep 25, 2013 at 13:41
  • 1
    implement your own card class and implement comparable. have a look at stackoverflow.com/questions/3320678/… you are not the first that implemented this ;-) Commented Sep 25, 2013 at 13:46
  • Instead of a Collection use Enums for the color and value of cards. Enums are using the order in which the values are declared, so no sorting needed. Commented Sep 25, 2013 at 13:52
  • use a map and give key values from 1-52 to every card , thus you can identify all of it Commented Sep 25, 2013 at 13:57
  • @HussainAkhtarWahid i wouldn't do that.... too much work... this is also a source for tricky bugs Commented Sep 25, 2013 at 14:39

4 Answers 4

1

Implement java.util.Comparator

public class CharacterComparator implements Comparator<Character> {
    public int compare(Character o1, Character o2) { 
        // ...

Then call sort

List<Character> characters = ...
Collections.sort(characters, new CharacterComparator());
Sign up to request clarification or add additional context in comments.

1 Comment

It took me a while to figure out how to use the comparator, but I got it working and it does what I want. Thanks.
1
   Collections.sort(list, new Comparator<String>() {
        public int compare(String a, String b) {
            //write here the easy code to provide you the right order
            //return -1 if a should appear before b or 1 otherwise
        }

    });

Comments

1

You need to implement your own comparator

public class CardComparator implements Comparator<Character>{
  public int compare(Character c1, Character c2){
   //implement your own logic for comparing.
   }
}

Once its implemented, you should invoke

Collections.sort(clubs, new CardComparator);

Comments

0

As an alternative solution to implementing your own Comparator consider using Google Guava. This library contains many helpful utility classes and functions.

It provides the class Ordering which makes this quite simple:

import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;

//explicitly state the ordering
Ordering<String> ordering = Ordering.explicit("A","1","2","K");

//create unsorted list
List<String> list = Lists.newArrayList("1","1","A","2","K","K","1","A");

System.out.println(ordering.sortedCopy(list)); //sort, but not in place

Collections.sort(list, ordering); //in place sort
System.out.println(list); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.