在android中排序字符串arraylist [英] sorting string arraylist in android

查看:31
本文介绍了在android中排序字符串arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string arraylist called names. How do I sort the arraylist in alphabetical order?

推荐答案

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

根据其元素的自然顺序,将指定列表按升序排序.列表中的所有元素都必须实现 Comparable 接口.此外,列表中的所有元素必须相互比较(即,e1.compareTo(e2) 不得为列表中的任何元素 e1 和 e2 抛出 ClassCastException).

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 类字符串

java.lang Class String

java.lang.Object 扩展为java.lang.String

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

所有实现的接口:可序列化、CharSequence、可比较

All Implemented Interfaces: Serializable, CharSequence, Comparable

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

这篇关于在android中排序字符串arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆