How can I sort an ArrayList lexicographically?

I am trying to sort an ArrayList of strings that represent map values. So, some cards contain letters ("King"), and some contain lines containing only a number ("7"). 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?

Edit: Sorry, I shouldn't have paid much attention when I watched the sort. The variety is working correctly, I must have just been discarded by the fact that 10 will come before 2. Thanks

+6
java string sorting arraylist lexicographic
source share
5 answers

No, Collections.sort will sort everything using Unicode ordinal lexicographic comparison, like the behavior of String.compareTo . "7" will come before the "King", and "10" will come before "2".

+9
source share

As I understand it, you have an array like ["7", "Queen", "9", "6"] , and you want it to look like ["Queen", "9", "7", "6"] (or in reverse) after sorting.

I would recommend making it more object oriented, i.e. create a class Card with the name and value of the field:

 class Card { private final String name; private final int value; ... //constructor and getters } 

and after that create instances like this:

 Card six = new Card("6", 6); Card ten = new Card("10", 10); Card queen = new Card("Queen", 12); 

After that, it will be much easier to perform all operations with maps (and, in particular, sorting) using the value field instead of map names.

+4
source share

As @Jon Skeet explained, inline sorting will compare based on Unicode values. You will have to write your own sorting method.

If you write your own code, can I suggest an enumeration? A deck of cards is one of the canonical examples of using enumerations. The short option is that you can declare your own sort order for a group of things; you could even get the king of spades to get ahead of the king of diamonds if you want. Check out the Sun tutorial here .

+1
source share

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 both are numbers if( a.matches("\\d+") && b.matches("\\d+")) { return new Integer( a ) - new Integer( b ); } // else, compare normally. return a.compareTo( b ); } }); System.out.println( list ); } } $ java Sort [2, 3, 7, 8, 10, A, Joker, Kings, Queen] 

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 ) { // add custom logic to compare one card with other } } 
0
source share

Sort will sort everything according to your encoding. In other words, all numbers will appear before the letters in lexicographical order. For example, decimal numbers begin with the character '.' and out of order lexicographically.

If you want to change this, make a Comparator object. Then you can place the items in any order.

For example, this will sort numbers in order of numbers, as well as words in lexical order:

 class CardComparator extends Object implements Comparator{ public int compare(Object a, Object b){ try{ double d1=Double.valueOf(a.toString()); try{ double d2=Double.valueOf(b.toString()); return (d2>d1)?1:-1; // both numeric }catch(NumberFormatException e){ // a is numeric but b isn't return 1; } }catch(NumberFormatException e){ try{ double d2=Double.valueOf(b.toString()); return -1; // a is nonnumeric but b is }catch(NumberFormatException e){ // both nonnumeric return a.toString().compareTo(b.toString); } } } } Comparator comparator=new CardComparator(); Collections.sort(cards, comparator); 

PS not verified!

0
source share

All Articles