简体   繁体   English

在树集中按字母顺序排序不起作用

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM