如何正确排序大小写字符串数组中的Android中? [英] How to properly sort upper and lowercase Strings in Array in Android?
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/5815423/sorting-arraylist-in-android-in-alphabetical-order-case-insensitive\">Sorting ArrayList的Android中的字母顺序(不区分大小写)
大家好我在整理一个小问题。
Hi everybody I have a small problem in sorting.
我正在String类型的一个集合。当我整理它给我的错误的结果,我期待
I am taking one collection of type String. When I am sorting it is giving me the wrong result that I was expecting
下面是我的code:
List <String> caps = new ArrayList<String>();
caps.add("Alpha");
caps.add("Beta");
caps.add("alpha1");
caps.add("Delta");
caps.add("theta");
Collections.sort(caps);
这是排序如下:
阿尔法
Beta版
三角洲
ALPHA1
THETA。
Alpha Beta Delta alpha1 theta.
这可以采取任何类型的字符串,甚至大/小写的。但小写的话是后来来了。
Here it can take any type of string even uppercase / lowercase. But the lowercase words are coming later.
我要出去放像:
阿尔法
ALPHA1
Beta版
三角洲
THETA
Alpha alpha1 Beta Delta theta
有一个简单的内置方法呢?
Is there an easy built-in method for this?
推荐答案
Collections.sort();
可以传递一个自定义的比较订货。对于不区分大小写排序字符串
类提供了一种称为静态最终比较 CASE_INSENSITIVE_ORDER
。
Collections.sort();
lets you pass a custom comparator for ordering. For case insensitive ordering String
class provides a static final comparator called CASE_INSENSITIVE_ORDER
.
因此,在这种需要的所有的情况是:
So in your case all that's needed is:
Collections.sort(帽,String.CASE_INSENSITIVE_ORDER);
这篇关于如何正确排序大小写字符串数组中的Android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!