1. package mySort;
    2. import java.util.Arrays;
    3. public class CompareTest {
    4. public static void main(String[] args) {
    5. test1();
    6. test2();
    7. }
    8. /**
    9. ComparabLe接口的使用举例: 自然排序
    10. 1.像String、包装类等实现了Comparable接口,重写了compareTo()方法,给出了两个对象大小的比较方式
    11. 2.像String、包装类重写compareTo()方法以后,进行了从小到大的排列
    12. 3.重写compareTo(obj)的规则:
    13. 如果当前对象this大于形参对象obj,则返回正整数,
    14. 如果当前对象this小于形参对象obj,则返回负整数,
    15. 如果当前对象this等于形参对象obj,则返回零。
    16. 4.对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable接口,重写了compareTo()方法。
    17. 需要在compareTo(obj)方法中指明如何排序
    18. */
    19. public static void test1(){
    20. String[] arr = new String[]{"AA","VV","MM","GG","BB"};
    21. Arrays.sort(arr);
    22. System.out.println(Arrays.toString(arr));
    23. }
    24. public static void test2(){
    25. Goods[] arr = new Goods[4];
    26. arr[0] = new Goods("WTY1",124);
    27. arr[1] = new Goods("WTY2",24);
    28. arr[2] = new Goods("WTY3",326);
    29. arr[3] = new Goods("WTY4",128);
    30. Arrays.sort(arr);
    31. System.out.println(Arrays.toString(arr));
    32. }
    33. }
    34. class Goods implements Comparable{//商品类
    35. private String name;
    36. private double price;
    37. public Goods() {
    38. }
    39. public Goods(String name, double price) {
    40. this.name = name;
    41. this.price = price;
    42. }
    43. public String getName() {
    44. return name;
    45. }
    46. public double getPrice() {
    47. return price;
    48. }
    49. public void setName(String name) {
    50. this.name = name;
    51. }
    52. public void setPrice(double price) {
    53. this.price = price;
    54. }
    55. @Override
    56. public String toString() {
    57. return "Goods{" +
    58. "name='" + name + '\'' +
    59. ", price=" + price +
    60. '}';
    61. }
    62. //指明商品比较大小的方式:
    63. //价格从低到高排序,再按照产品名称从高到低排序
    64. @Override
    65. public int compareTo(Object o) {
    66. if(o instanceof Goods){
    67. Goods goods = (Goods) o;
    68. //方式一:
    69. if(this.price > goods.price){
    70. return 1;
    71. }else if(this.price < goods.price){
    72. return -1;
    73. }else {
    74. return -this.name.compareTo(goods.name);//前面加了一个负号
    75. }
    76. //方式二:
    77. //return Double.compare(this.price,goods.price);
    78. }
    79. throw new RuntimeException("传入的数据类型不一致! ");
    80. }
    81. }

    image.png

    1. package mySort;
    2. import java.util.ArrayList;
    3. import java.util.Collections;
    4. public class DIYsort {
    5. public static void main(String[] args) {
    6. ArrayList<Shape> list = new ArrayList<>();
    7. Circle circle1 = new Circle(2.0);
    8. Circle circle2 = new Circle(3.0);
    9. Triangle triangle1 = new Triangle(1, 2);
    10. Triangle triangle2 = new Triangle(2, 2);
    11. list.add(circle1);
    12. list.add(circle2);
    13. list.add(triangle1);
    14. list.add(triangle2);
    15. Collections.sort(list);
    16. list.forEach(e -> {
    17. System.out.println(e.getArea());
    18. });
    19. }
    20. }
    21. interface Shape extends Comparable<Shape>{
    22. double getArea();
    23. @Override
    24. int compareTo(Shape o);
    25. }
    26. class Circle implements Shape{
    27. private double r;
    28. public Circle() {
    29. }
    30. public Circle(double r) {
    31. this.r = r;
    32. }
    33. @Override
    34. public double getArea() {
    35. return Math.PI * r * r;
    36. }
    37. @Override
    38. public int compareTo(Shape o) {
    39. Double a1 = getArea();
    40. Double a2 = o.getArea();
    41. return a1.compareTo(a2);
    42. }
    43. }
    44. class Triangle implements Shape{
    45. private double length;
    46. private double width;
    47. public Triangle() {
    48. }
    49. public Triangle(double length, double width) {
    50. this.length = length;
    51. this.width = width;
    52. }
    53. @Override
    54. public double getArea() {
    55. return length * width;
    56. }
    57. @Override
    58. public int compareTo(Shape o) {
    59. Double a1 = getArea();
    60. Double a2 = o.getArea();
    61. return a1.compareTo(a2);
    62. }
    63. }

    image.png