自然排序与定制排序

Posted ylblikestudyjava

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自然排序与定制排序相关的知识,希望对你有一定的参考价值。

自然排序:实现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));
    }

 

以上是关于自然排序与定制排序的主要内容,如果未能解决你的问题,请参考以下文章

Java自用集合练习题:TreeSet的自然排序与定制排序

TreeSet之定制排序和自然排序

一文真正搞懂Java中的自然排序和定制排序,到底升序还是降序

java TreeSet的排序之定制排序

TreeMap与Properties

7.5 Queue集合