java 数组排序 Comparator

public class HomeWork01 {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100d);
        books[1] = new Book("金瓶梅", 200d);
        books[2] = new Book("青年文摘", 5d);
        books[3] = new Book("javaAndSpring", 300d);

//        ArraySortByPrice(books, new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                Double d1 = (Double)o1;
//                Double d2 = (Double)o2;
//                int i = (int)(d2 - d1);
//                return i;
//            }
//        });

        // 按价格排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Double price1 = o1.getPrice();
                Double price2 = o2.getPrice();
                double price3 = price1 - price2;
                if (price3 > 0) {
                    return 1;
                } else if (price3 < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });

        //按书名长度排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                // 调整o1和o2的顺序可以调整排序方式(从大到小,或者从小到大)
                return o1.getName().length() - o2.getName().length();
            }
        });


        System.out.println(Arrays.toString(books));

    }


    // 自定义排序
    public static void ArraySortByPrice(Book[] book, Comparator c) {
        Book temp = null;
        for (int y = 0; y < book.length - 1; y++) {
            for (int i = 0; i < book.length - 1 - y; i++) {
                if (c.compare(book[i].getPrice(), book[i + 1].getPrice()) > 0) {
                    temp = book[i];
                    book[i] = book[i + 1];
                    book[i + 1] = temp;
                }
            }
        }
    }

    // 自定义排序
    public static void ArraySortByBookName(Book[] book) {
        Book temp = null;
        for (int y = 0; y < book.length - 1; y++) {
            for (int i = 0; i < book.length - 1 - y; i++) {
                if (book[i].getName().length() > book[i + 1].getName().length()) {
                    temp = book[i];
                    book[i] = book[i + 1];
                    book[i + 1] = temp;
                }
            }
        }
    }

}

class Book {
    private String name;
    private Double price;

    public Book(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public Double getPrice() {
        return price;
    }

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

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

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
上一篇:【Java 8 新特性】Java 8 Stream使用sorted()方法自定义排序的示例


下一篇:List操作