对象数组 排序-根据其中一个属性

     排序就不再调用Comparator接口,而是实体类中实现接口Comparable

在实体类中重写compare方法
模型类Fruit

复制代码

public class FruitBusiness implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        Fruit f1 = (Fruit)o1;
        Fruit f2 = (Fruit)o2;
        if(f2.getPrice() > f1.getPrice())
            return 1;// 正数
        else if(f1.getPrice() > f2.getPrice())
            return -1;// 负数
        else
            return 0;
    }
}
//
Arrays.sort(array,new FruitComparator());

复制代码

//代码框模板

复制代码
//
import java.util.Comparator;
public class IntComparator implements Comparator {
    /**
     * 返回0表示相等 返回正数表示前面的数大 返回负数表示后面的数大
     */
    @Override
    public int compare(Object arg0, Object arg1) {
        int x = (Integer)arg0;
        int y = (Integer)arg1;
        return y-x;
    }
}
//或者实体类中重写
public int compareTo(Object o) {
        Fruit fruit = (Fruit)o;
        if(fruit.getPrice() > this.price)
            return 1;
        else if(this.price > fruit.price)
            return -1;
        else
            return 0;
    }
//调用
//public Fruit(String name, int count, double price) {}
Fruit[] array = {
        new Fruit("苹果",1000, 10.0),
        new Fruit("香蕉",200,3),
        new Fruit("榴莲", 100, 20),
        new Fruit("菠萝", 20000, 0.5),
        new Fruit("梨子", 2000, 2),
        new Fruit("板栗", 700, 9)
        };
//
//Arrays.sort(array);

复制代码

//代码框模板

复制代码
我是想先把对象存到数组里面,然后再对数组里面的对象排序,通过我的比较方式,排序。。。 
------Solutions------
要么实现 Comparable,使用 Arrays.sort(array) 排序

创建一个指定了比较器的comparator排序
要么 实现一个 Comparator 使用 Arrays.sort(array, comparator) 排序。
------Solutions------

import java.util.*;

public class CompType implements Comparable<CompType> {//数组中的元素实现Comparable接口
    private int i, j;
    public CompType(int i, int j) {
this.i = i;
this.j = j;
    }
    public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素
return "[" + i + "," + j + "]";
    }
    public int compareTo(CompType rv) {//实现这个接口方法,用于数组中元素优先级的比较,我只考虑i的大小
return i < rv.i ? -1 : (i == rv.i ? 0 : 1);
    }
public static void main(String[] args) {
CompType[] a = { 
new CompType(1, 3), new CompType(5, 5),
new CompType(4, 4), new CompType(2, 1),
new CompType(3, 2), new CompType(4, 7), 
new CompType(6, 7), new CompType(7, 3),
new CompType(1, 6), new CompType(1, 1), };
System.out.println("before sort:" + Arrays.toString(a));
Arrays.sort(a);
System.out.println("after  sort:" + Arrays.toString(a));
    }
}//
//结果:
//before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]]
//after  sort:[[1,3], [1,6], [1,1], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]



import java.util.*;

public class CompType{
    private int i, j;
    public CompType(int i, int j) {
this.i = i;
this.j = j;
    }
    public String toString() {//这个方法无关紧要,我只是为了输出形式美观一点而重写它,如果你不重写,你可以利用for循环打印数组元素
return "[" + i + "," + j + "]";
    }
    class CmpTypeComparator implements Comparator<CompType> {
public int compare(CompType o1, CompType o2) {//实现这个接口方法,用于数组中元素优先级的比较,我同时考虑了i,j
   if(o1.i<o2.i) return -1;
   else if(o1.i==o2.i) {
       if(o1.j<o2.j) return -1;
       else if(o1.j==o2.j)return 0;
   }
   return 1;    
}
    }
    public static void main(String[] args) {
CompType[] a = { 
new CompType(1, 3), new CompType(5, 5),
new CompType(4, 4), new CompType(2, 1),
new CompType(3, 2), new CompType(4, 7), 
new CompType(6, 7), new CompType(7, 3),
new CompType(1, 6), new CompType(1, 1), };
System.out.println("before sort:" + Arrays.toString(a));
Arrays.sort(a,new CompType(0,0).new CmpTypeComparator());
System.out.println("after  sort:" + Arrays.toString(a));
    }
}//
//结果:
//before sort:[[1,3], [5,5], [4,4], [2,1], [3,2], [4,7], [6,7], [7,3], [1,6], [1,1]]
//after  sort:[[1,1], [1,3], [1,6], [2,1], [3,2], [4,4], [4,7], [5,5], [6,7], [7,3]]
复制代码
posted @ 2017-07-05 09:29  李杼遥  阅读(155)  评论(0编辑  收藏  举报