我正在进行自动化,尝试执行以下操作:
这些是我从网站提升后得到的价值观:
_john
_tim
11
111
5
A
aaa
aaa
AI-1
Android
API
AppName
asd
AWS
AWS
awstest
AWSTest1type
Azure在使用java集合进行排序之后,我得到如下所示:
_john
_tim
11
111
5
A
AI-1
API
AWS
AWS
Android
AppName
Azure
aaa
aaa用于排序的代码:
String rules = "< '_' < 1 < 2 <3 <4 < 5 < A < a";
Collections.sort(myStringArray, new RuleBasedCollator(rules));我可以在上面的规则中遗漏任何东西吗?
发布于 2019-01-19 05:59:36
这是由于ASCII字符集的顺序,其中'Z‘排在'a’之前。
RuleBasedCollator只检查要排序的字符串的第一个字母。你所设定的规则是(A < a)。它首先列出以大写字母开头的所有字符串,然后是小写字母。
由于您的列表包含以特殊字符开头的字符串,我建议创建两个列表进行排序。一个用于字符串,以特殊字符开头,然后为包含所有其他值的其他列表。分别对这两个列表进行排序,然后合并排序的列表。我已经尝试了下面的代码,它运行良好。
// Input list
List<String> name = new ArrayList<String>();
final String[] specialChars = { "_", ">" };
List<String> specCharList = new ArrayList<String>();
List<String> strList = new ArrayList<String>();
List<String> finalList = new ArrayList<String>();
String rules = "< '_' < '>' ";
boolean isSpec = false ;
for(String names : name) {
isSpec = false ;
for(int i=0;i<specialChars.length;i++) {
if(names.startsWith(specialChars[i])) {
// System.out.println("Name : "+names);
isSpec = true ;
}
}
// to sort special char list and normal list
if(isSpec) {
specCharList.add(names);
} else {
strList.add(names);
}
}
try {
// To sort special character list
Collections.sort(specCharList, new RuleBasedCollator(rules));
// Add the sorted list to finallist
finalList.addAll(specCharList);
// to sort other list
Collections.sort(strList, String.CASE_INSENSITIVE_ORDER);
// Add the sorted list to finallist
finalList.addAll(strList);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Final Output List --------");
for(String names : finalList) {
System.out.println(names);
}https://stackoverflow.com/questions/54264147
复制相似问题