关于java的排序规则写法

来源:互联网 发布:网络歌手侃侃 编辑:程序博客网 时间:2024/05/17 12:48

 

关于java排序
这里给出了两个示例
对于复杂对象或者问题,核心其实就是定义一个比较器
本菜对java还不是很熟练,定义和使用比较器的方法比较笨
这里有一个关于比较器的文章,写的还不错
http://wenku.baidu.com/view/b8bca58371fe910ef12df8d0.html
JAVA CODE   :No Title Code
  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106
import java.io.*;import java.util.*;import java.math.*;import java.text.*;import java.util.Comparator;public class Main{   final static int maxn=100;                     public static class student{       String name;       int score;       student(){};       student(String nn,int ss){       name=nn;       score=ss;       }       String getname(){       return name;       }       int getscore(){       return score;       }       void output(){       System.out.println(name+" "+score);       }       };              public static class MyComparator implements Comparator{            public int compare(Object  obj1, Object obj2) {            student x=(student)obj1;            student y=(student)obj2;      if(x.getscore()!=y.getscore())  return x.getscore()<y.getscore()?1:-1;    return x.getname().compareTo(y.getname())>0?1:-1;            }       };              public static student p[]=new student[maxn];        public static Integer a[]=new Integer[maxn];                     public static void main(String args[]){              Scanner in=new Scanner(System.in);                                                        ///排序1:数组排序              //示例数据              //5              //4 1 3 6 9              int n=in.nextInt();              for(int i=0;i<n;i++)a[i]=in.nextInt();              //第1个参数传入的是数组的首地址例如a              //接下来传入的是排序的范围 x,y,表示将区间a[x]到a[y-1]段排序              //默认是从小到大排              Arrays.sort(a,0,n);              for(int i=0;i<n;i++)System.out.print(a[i]+" ");              System.out.println();              //这是构造方法类实现排序准则的形式              Arrays.sort(a,0,n,new Comparator<Integer>(){              public int compare(Integer x,Integer y){              return x<y?1:-1;//表示从大到小              }              });              for(int i=0;i<n;i++)System.out.print(a[i]+" ");              System.out.println();                                                        //排序2:类排序              //示例数据              //5              //xiaoming 100              //dong 100              //haha 75              //hehe 80              //xiaowang 90              int m=in.nextInt();              for(int i=0;i<m;i++){              String name=in.next();              int score=in.nextInt();              p[i]=new student(name,score);              }              //示例:首先按成绩从高到底,若相同,按名字字典序              Arrays.sort(p,0,m,new Comparator<student>(){              public int compare(student x,student y){              if(x.getscore()!=y.getscore())              return x.getscore()<y.getscore()?1:-1;              return x.getname().compareTo(y.getname())>0?1:-1;              }              });                                          //这是外部定义比较器类的使用方法              Arrays.sort(p,0,m,new MyComparator());              for(int i=0;i<m;i++)p[i].output();                     }}