如何按字典顺序对 ArrayList 进行排序? [英] How do I sort an ArrayList lexicographically?

查看:43
本文介绍了如何按字典顺序对 ArrayList 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对代表卡片值的字符串数组进行排序.因此,有些卡片包含字母(King"),有些卡片包含仅包含数字(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 将使用 Unicode 序数字典比较对所有内容进行排序,因为这是 String.compareTo.7"将在King"之前,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆