[英]Alphabetical sorting in treeset not working
Hi, my code is like this: 嗨,我的代码是这样的:
TreeSet<String> ts=new TreeSet<String>();
ts.add("Testtxt");
ts.add("Testxml");
ts.add("docdoc");
ts.add("ePeoplexml");
ts.add("fantasyxlsx");
ts.add("idaddedgif");
ts.add("idaddedrtf");
System.out.println("Tree set :: "+ts);
Output: 输出:
Tree set :: [Testtxt, Testxml, docdoc, ePeoplexml, fantasyxlsx, idaddedgif, idaddedrtf]
It's not sorting all strings in alphabetical order.Can any one help how to achieve an ascending order of the strings in treeset
. 它不按字母顺序对所有字符串进行排序。任何人都可以帮助如何在
treeset
实现字符串的treeset
。
Thanks Madhu. 谢谢Madhu。
The sorting is fine. 排序很好。 It is done in case-sensitive manner.
它以区分大小写的方式完成。 Since unicode code point of
T
comes before d
, so, Testtxt
comes before docdoc
in sorted set. 由于
T
unicode代码点在d
之前,因此, docdoc
在排序集中的Testtxt
之前docdoc
。
Since you want to do case insensitive sorting, you can use a pre-defined static CASE_INSENSITIVE comparator defined in String
class. 由于您希望进行不区分大小写的排序,因此可以使用
String
类中定义的预定义静态CASE_INSENSITIVE比较器。 Instantiate your TreeSet
like this: 像这样实例化
TreeSet
:
TreeSet<String> ts=new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.