I know what to use Collections.sort, but it only sorts strings containing letters. How to get ArrayList to sort by number as well as alphabetically?
If the string is a number, it is already sorted (like String though):
import java.util.*; class Sort { public static void main( String [] args ) { List list = Arrays.asList("Kings","7", "Abcd", "3.1416"); Collections.sort( list ); System.out.println( list ); } }
Print
$ java Sort [3.1416, 7, Abcd, Kings]
That's what you need?
change
Assuming (guessing) what you need, you need to sort the deck of cards, which has both numbers and letters (J, Q, K, A), you can try to use a custom comparator.
Here is one that takes numbers βas numbers,β the rest as strings, so β10β occurs after β2,β but before βKingsβ
import java.util.*; class Sort { public static void main( String [] args ) { List<String> list = Arrays.asList("Kings","7", "Queen", "3", "10", "A", "2", "8", "Joker"); Collections.sort( list , new Comparator<String>(){ public int compare( String a, String b ){
If this is what you need, I think it will help you figure out the rest. Probably the next thing will be how to sort the peaks against the hearts.
After the Roman answer, you can create a class and implement Comparable :
class Card implements Comparable<Card> { public int compareTo( Card other ) {