第一种方法:容器内要排序的类必须时下Comparable接口: sort ( List list)
例子:
import java.util.*;
public class Main{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(2,"aa"));
al.add(new Student(1,"bb"));
al.add(new Student(3,"dd"));
al.add(new Student(3,"cc"));
Collections.sort(al);
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Student implements Comparable{
int id;
String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
public int compareTo(Object o){
Student s=(Student)o;
int result=(id>s.id)?1:((id==s.id)?0:-1);
if(0==result){
result=name.compareTo(s.name);
}
return result;
}
public String toString(){
return "id="+this.id+",name="+this.name;
}
}
第二种方法:使用静态内部类实现Comparator接口:sort ( List list, Comparator c)
只需实现compare方法就行,equals方法在obeject类就会有,而实体类继承自object类,就必然会有equals方法,所以不需实现
例子
import java.util.*;
public class Main{
public static void main(String args[]){
ArrayList al=new ArrayList();
al.add(new Student(2,"aa"));
al.add(new Student(1,"bb"));
al.add(new Student(3,"dd"));
al.add(new Student(3,"cc"));
Collections.sort(al,new StudentComparator());
Iterator it=al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
class Student {
int id;
String name;
Student(int id,String name){
this.id=id;
this.name=name;
}
public String toString(){
return "id="+this.id+",name="+this.name;
}
}
class StudentComparator implements Comparator{
public int compare(Object o1,Object o2){
Student s1=(Student)o1;
Student s2=(Student)o2;
int result=(s1.id>s2.id)?1:((s1.id==s2.id)?0:-1);
if(0==result){
result=s1.name.compareTo(s2.name);
}
return result;
}
}
分享到:
相关推荐
NULL 博文链接:https://xuedong.iteye.com/blog/1147254
主要介绍了Java Arrays.sort和Collections.sort排序实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了java中Collections.sort排序详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了Java Collections.sort()实现List排序的默认方法和自定义方法,需要的朋友可以参考下
主要介绍了JAVA对list集合进行排序Collections.sort(),需要的朋友可以参考下
本篇文章主要介绍了java中Collections.sort排序函数用法详解,非常具有实用价值,需要的朋友可以参考下。
本文通过两种方法给大家介绍java集合中的Collections.sort方法对list排序,第一种方式是list中的对象实现Comparable接口,第二种方法是根据Collections.sort重载方法实现,对collections.sort方法感兴趣的朋友一起...
Comparator是个接口,可重写compare()及equals()这两个方法,接下来通过本文给大家介绍Java中Collections.sort排序,需要的的朋友参考下吧
主要介绍了Java Collections.sort()排序代码案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了详解java Collections.sort的两种用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
Android利用Collections.sort()对Map里的键值对按照键进行排序,让listview按序显示
字符串数组 排序
java 集合类的排序主要是用Collections.sort方法,Collections和Collection是不一样的,前者是类,后者是接口,在这里,我主要是想说明它的sort方法的几种类型, 提示:实现接口的方法时,只需要比较两个数,大的返回1,...
主要介绍了JAVA中Collections工具类sort()排序方法,非常具有实用价值,需要的朋友可以参考下。
用百Collections.reverse(list)即可度。}//先升序排序Collections.sort(list);//再反转Collections.r
listview 显示数据 子view里有多个view 使用adapter,Collections.sort方式遍历map里的数据
using System.Collections.Generic;using System.Linq; using System.Text; using System.Data ; namespace Sale_Core { public class SortPags { /// /// 存储传入的数据 /// private DataTable _
排序集合类型脚本
List 和 Set 的区别,final finally finalize,Java获取反射的三种方法,Arrays.sort 和 Collections.sort 实现原理 和区别
10-集合(Collections-sort).avi