码迷,mamicode.com
首页 > 编程语言 > 详细

自然排序与定制排序

时间:2020-03-01 10:35:52      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:mat   类型   imp   instance   gen   苹果   cep   匿名   throw   

自然排序:实现Comparable接口,自定义重写compareTo方法

//编写商品类,实现Comparable接口

package compare;

import static org.hamcrest.CoreMatchers.instanceOf;

public class Goods implements Comparable{
    private String name;
    private double price;
    
    
    
    public Goods() {
        super();
        // TODO Auto-generated constructor stub
    }



    public Goods(String name, double price) {
        super();
        this.name = name;
        this.price = price;
    }



    public String getName() {
        return name;
    }



    public void setName(String name) {
        this.name = name;
    }



    public double getPrice() {
        return price;
    }



    public void setPrice(double price) {
        this.price = price;
    }



    @Override
    public String toString() {
        return " [name=" + name + ", price=" + price + "]";
    }

    /*
     * 如果返回值为正数:表示调用此方法的对象大
     * 如果返回值为负数:表示调用此方法的对象小
     * 如果返回值为零:表示两个对象一样大
     */

    @Override
    public int compareTo(Object o) {
        if (this==o) {
            return 0;
        }
    if (o instanceof Goods) {
        Goods g1=(Goods) o;
        
        if ( Double.compare(this.price, g1.price)!=0) {
            return Double.compare(this.price, g1.price);
        }else {
            return this.name.compareTo(g1.name);
        }
    }else {
        throw new RuntimeException("输入类型不匹配");
    }
    }
    
    
}

 

//测试自然排序

public void test2() {
        Goods g1=new Goods("苹果", 9899);
        Goods g2=new Goods("锤子", 2099);
        Goods g3=new Goods("360", 1009);
        Goods g4=new Goods("Samsung", 9999);
        Goods g5=new Goods("red米", 999);
        Goods g6=new Goods("OPPO Reno", 1099);
        
        Goods [] arr=new Goods [] {g1,g2,g3,g4,g5,g6};
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

 

定制排序:new 一个实现接口Comparator的类的对象,重写他的compare方法,自定义排序

这里直接写的是他的匿名对象

public void test3() {
        String [] arr =new String [] {"ww","xx","mm","aa","zz"};
        
        System.out.println("显示未排序的原数组"+Arrays.toString(arr));
        
        Arrays.sort(arr,new Comparator(){
            //指明对象的排序方式
            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof String && o2 instanceof String) {
                    String s1=(String)o1;
                    String s2=(String) o2;
                    return -s1.compareTo(s2);
                }else {
                    throw new RuntimeException();
                }
                
            }
            
        });
        System.out.println("排序后"+Arrays.toString(arr));
    }
    
    @Test
    public void test4() {
        Goods g1=new Goods("苹果", 9899);
        Goods g2=new Goods("锤子", 2099);
        Goods g3=new Goods("360", 1009);
        Goods g4=new Goods("Samsung", 9999);
        Goods g5=new Goods("red米", 999);
        Goods g6=new Goods("OPPO Reno", 1099);
        
        Goods [] arr=new Goods [] {g1,g2,g3,g4,g5,g6};
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
        Arrays.sort(arr, new  Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                System.out.println("comparetor()....");
                if (o1 instanceof Goods && o2 instanceof Goods) {
                    Goods s1=(Goods)o1;
                    Goods s2=(Goods)o2;
                    if ( Double.compare(s1.getPrice(), s2.getPrice())!=0) {
                        return Double.compare(s1.getPrice(), s2.getPrice());
                    }else {
                        return   -Double.compare(s1.getPrice(), s2.getPrice());
                    }
                }else {
                    throw new RuntimeException();
                }
            
                
            }
            
        });
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
    }

 

自然排序与定制排序

标签:mat   类型   imp   instance   gen   苹果   cep   匿名   throw   

原文地址:https://www.cnblogs.com/ylblikestudyJava/p/12388453.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!