java collections.sort 忽略大小写排序,排序列表时,Android的忽略大小写

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

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 {

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值