我如何排序字典顺序一个ArrayList? [英] How do I sort an ArrayList lexicographically?
问题描述
我想那个排序重新present卡值的字符串的ArrayList。所以,有些卡包含字母(王)和一些只包含包含数字(7)的字符串。我知道使用Collections.sort,但它只是排序包含字母的字符串。我如何获得该ArrayList以数字和字母顺序进行排序?
I am trying to sort an ArrayList of Strings that represent card values. So, some cards contain letters ("King") and some contain Strings containing only a number ("7"). I know to use Collections.sort, but it only sorts Strings that contain letters. How do I get the ArrayList to be sorted by number as well as alphabetically?
编辑:对不起,我不能一直十分重视,当我看着排序。排序工作正常,我一定是刚刚被这样一个事实:10将之前2.感谢来揭去
Sorry, I must not have been paying much attention when I looked at the sorting. The sort works correctly, I must have just been thrown off by the fact that a 10 will come before a 2. Thanks
推荐答案
没有, Collections.sort
将整理一切,用统一code序号词典作为对比这就是<一个行为href=\"http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo%28java.lang.String%29\"><$c$c>String.compareTo$c$c>. 7会来王面前,10会2了。
No, Collections.sort
will sort everything, using an Unicode ordinal lexicographic comparison as that's the behaviour of String.compareTo
. "7" will come before "King", and "10" will come before "2".
这篇关于我如何排序字典顺序一个ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!