Fork me on GitHub

java结构体排序

import java.util.Arrays;
import java.util.Scanner;


public class aa {
    public static class stu implements Comparable{
        int a;
        int b;
        stu(){}//无参构造
        stu(int a,int b){
            this.a = a;
            this.b = b;
        }
        @Override
        public int compareTo(Object o){
            stu s = (stu) o;
            return a - s.a;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        stu[] no = new stu[4];
        no[0] = new stu(2,3);
        no[1] = new stu(5,99);
        no[2] = new stu(-43,88);
        no[3] = new stu(66,30);
        Arrays.sort(no);
        for(int i = 0;i < no.length;i++){
            System.out.println(no[i].a);
        }
        return;
    }

}

 

posted @ 2018-04-15 16:23  梦想飞的菜鸟  阅读(1517)  评论(1)    收藏  举报