赞
踩
本问题已经有最佳答案,请猛点这里访问。
我有一个名为名称的字符串arraylist。 如何按字母顺序对数组列表排序?
杜德(Dude),谷歌" sort arraylist java"。 第一次点击会显示答案。 这个问题与Android(纯Java)无关。
@Flo确实是,但是后来我尝试在此处链接一个dupe,并且一切都变得更加复杂,对于基本的东西我找不到任何问题(关闭是对ArrayList进行排序)。 可能在这里,但我可能错过了。
ArrayList names = new ArrayList();
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
另一种解决方案,但Collections.sort()是最好的。 我只想显示替代
ArrayList strings = new ArrayList();
strings.add("a");
strings.add("ab");
strings.add("aa");
String[] stringsArray = new String[strings.size()];
strings.toArray(stringsArray);
Arrays.sort(stringsArray);
List sorted = Arrays.asList(stringsArray);
System.out.println(sorted);
导入java.util.Collections;
然后使用Collections.sort();
我正在使用android platform。是否可以在android中使用相同的代码?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。