本文介绍了排序列表时,Android的忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表命名的路我正在整理我的琴弦用下面的code
I have a List named path I'm currently sorting my strings with the following code
java.util.Collections.sort(path);
这是工作的罚款它梳理我的名单但它把第一个字母的情况下,不同的是它排序列表大写字母,然后经过这么排序列表小写字母,如果我有以下的猫狗鸟斑马它会排序它像
That is working fine it sorts my list however it treats the cases of the first letter differently that is it sorts the list with upper-case letters and then sorts the list with lower-case letters after so if I had the following cat dog Bird Zebra it would sort it like
Bird
Zebra
dog
cat
让我怎么忽略的情况下,这样的狗和猫会来的Zebra之前,但伯德之后?感谢您的帮助
so how do I ignore case so that dog and cat would come before Zebra but after Bird?Thank you for any help
推荐答案
创建一个自定义比较器类:
Create a custom comparator class:
import java.util.Comparator;
class IgnoreCaseComparator implements Comparator<String> {
public int compare(String strA, String strB) {
return strA.compareToIgnoreCase(strB);
}
}
然后在你的排序:
Then on your sort:
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(path,icc);
这篇关于排序列表时,Android的忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!