Java的数组与集合排序问题
数组排序
先实现Comparable接口
重写compareTo函数
public class Main implements Comparable{
long sid;
int dgra;
int cgra;
int sum;
public int compareTo(Object o) {
Main p=(Main)o;
int n= sum<p.sum?1:(sum==p.sum?0:-1);
if(n==0)
{
n=dgra<p.dgra?1:(dgra==p.dgra?0:-1);
if(n==0)
n=sid>p.sid?1:-1;
}
return n;
}
public static void main(String[] args) {
Main[] stu2 = new Main[k];
Arrays.sort(stu1);
}
}
集合排序
先实现Comparable接口
重写compareTo函数
1. public class Main implements Comparable<Main>{
long sid;
int dgra;
int cgra;
int sum;
public int compareTo(Main o) {
Main p=o;
int n= sum<p.sum?1:(sum==p.sum?0:-1);
if(n==0)
{
n=dgra<p.dgra?1:(dgra==p.dgra?0:-1);
if(n==0)
n=sid>p.sid?1:-1;
}
return n;
}
public static void main(String[] args) {
List<Main> stu1 = new ArrayList<Main>();
Collections.sort(stu1);
}
}