android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)
2012-05-04 10:31
465 查看
Info.java :
在JAVA中 Collections.sort(List<T> list) 的测试:
结果:
1
2
3
4
5
在android 中 Collections.sort(List<T> list) 的测试:
结果:
3
2
1
4
5
在android中实现如果排序的方法可以用Collections.sort(List<T> list, new Comparator<Info>() {
可以实现对list的排序。
public class Info implements Cloneable, Comparable<Info>{ private int id ; public Info(int id ){ this.id = id ; } public int getId() { return id; } @Override public Object clone() throws CloneNotSupportedException { Info info = (Info) super.clone(); return info; } @Override public int compareTo(Info o) { if(this.id > o.id ){ return 1 ; } return 0; } }
在JAVA中 Collections.sort(List<T> list) 的测试:
List<Info> list = new ArrayList<Info>() ; list.add( new Info(3)) ; list.add( new Info(2)) ; list.add( new Info(1)) ; list.add( new Info(4)) ; list.add( new Info(5)) ; Collections.sort(list) ; for(int i = 0 ; i < 5 ; i ++){ System.out.println(list.get(i).getId()); }
结果:
1
2
3
4
5
在android 中 Collections.sort(List<T> list) 的测试:
List<Info> list = new ArrayList<Info>() ; list.add( new Info(3)) ; list.add( new Info(2)) ; list.add( new Info(1)) ; list.add( new Info(4)) ; list.add( new Info(5)) ; Collections.sort(list) ; for(int i = 0 ; i < 5 ; i ++){ System.out.println(list.get(i).getId()); }
结果:
3
2
1
4
5
在android中实现如果排序的方法可以用Collections.sort(List<T> list, new Comparator<Info>() {
@Override public int compare(Info lhs, Info rhs) { return rhs.getId() - lhs.getId(); } }
可以实现对list的排序。
相关文章推荐
- android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)
- android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)
- 【Java】如何给List集合排序Collections.sort(list,new Comparator<Entity>)
- Java List<>排序——重载Collections.sort方法
- 实体类集合(List<Object>)根据实体类的一个或者多个属性对集合进行排序-Collections.sort
- 使用Collections.sort(List<T>,Comparator<T>)给List<String>排序(一)
- java/android String和List<String>的区别
- Collections.sort(List<T> list, Comparator<? super T> c) 源代码分析
- Collections.sort(List<T> Comparator) 自定义排序
- comparator<> || Collections.sort -------------------------java
- 使用Collections.sort(List<T>,Comparator<? super T>)给List<String>排序(一)
- Android-在XML和Java代码中设置背景在不同状态的效果: <selector>/StateListDrawable
- 使用Collections.sort(List<T>,Comparator<T>)给List<String>排序(一)
- Collections.sort(List<T> list, Comparator<? super T> c) 应用
- 使用Collections.sort(List<T>,Comparator<? super T>)给List<String>排序
- Android中将List<Map<String, Object>>类型数据与字符串的相互转化
- android项目报错 org.JSONException:Value<html><head><title>Apache of type java.lang.String.
- java中list排序:Collections.sort() 排序函数的用法
- 剑指Offer——巧妙使用sort(List<T>,Comparator<? super T>)比较器
- STL经典算法集锦<一>之list::sort