android中数组自动排序,关于Java:在android中对字符串arraylist进行排序

本问题已经有最佳答案,请猛点这里访问。

我有一个名为名称的字符串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中使用相同的代码?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值