java - 在android中对字符串数组列表进行排序

标签 java sorting arraylist

我有一个名为names的字符串数组列表。如何按字母顺序对数组列表进行排序?

最佳答案

ArrayList<String> names = new ArrayList<String>();
names =fillNames() // whatever method you need to fill here;
Collections.sort(names);

http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29

Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list).

String 实现 Comparable:

java.lang Class String

java.lang.Object extended by java.lang.String

All Implemented Interfaces: Serializable, CharSequence, Comparable

http://download.oracle.com/javase/6/docs/api/java/lang/String.html

关于java - 在android中对字符串数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814791/

相关文章:

ios - 如何对项目进行排序,以便在 UICollectionView 的每一行上浪费尽可能少的空间?

java - 将按钮面板放在中央 Java Swing

java - 从 CellComment 检索 ClientAnchor 时的 POI 3.17 NPE

c++ - 如何排序 std::vector 忽略某些数字?

c# - 有条件的随机播放列表

c# - 将字符串转换为数组不起作用

java - 不可转换类型错误

java - 如何从句子中的数组列表中获取首次出现的任何单词的索引

javascript - 将自定义消息身份验证代码从 Java 转换为 JavaScript

java - 帮助处理java中的静态变量