1 package arrays; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 6 public class ArraysExercise01 { 7 public static void main(String[] args) { 8 Book[]books=new Book[4]; 9 books[0]=new Book("红楼梦",100); 10 books[1]=new Book("水浒传",90); 11 books[2]=new Book("青年文摘",5); 12 books[3]=new Book("java从入门到放弃",300); 13 14 Arrays.sort(books, new Comparator() { 15 @Override 16 public int compare(Object o1, Object o2) { 17 if (!(o1 instanceof Book)||!(o2 instanceof Book)){ 18 return 0; 19 } 20 Book b1=(Book)o1; 21 Book b2=(Book)o2; 22 //返回的正数还是负数,会影响排序的顺序 23 if(b1.getPrice()-b2.getPrice()>0){ 24 return 1; 25 }else if (b1.getPrice()-b2.getPrice()<0){ 26 return -1; 27 }else{ 28 return 0; 29 } 30 31 } 32 }); 33 34 System.out.println(Arrays.toString(books)); 35 } 36 } 37 class Book{ 38 private String name; 39 private double price; 40 41 public Book(String name, double price) { 42 this.name = name; 43 this.price = price; 44 } 45 46 public String getName() { 47 return name; 48 } 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 public double getPrice() { 55 return price; 56 } 57 58 public void setPrice(double price) { 59 this.price = price; 60 } 61 62 @Override 63 public String toString() { 64 return "Book[name="+name+",price="+price+"] "; 65 } 66 }
结果:
[Book[name=青年文摘,price=5.0] , Book[name=水浒传,price=90.0] , Book[name=红楼梦,price=100.0] , Book[name=java从入门到放弃,price=300.0] ]
1.上面的为实现Comparator接口匿名类,也成为定制排序
2.下面的是让Book实现Comparable接口,完成排序,也成为自然排序
1 package arrays; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 6 public class ArraysExercise01 { 7 public static void main(String[] args) { 8 Book[]books=new Book[4]; 9 books[0]=new Book("红楼梦",100); 10 books[1]=new Book("水浒传",90); 11 books[2]=new Book("青年文摘",5); 12 books[3]=new Book("java从入门到放弃",300); 13 14 /*Arrays.sort(books, new Comparator() { 15 @Override 16 public int compare(Object o1, Object o2) { 17 if (!(o1 instanceof Book)||!(o2 instanceof Book)){ 18 return 0; 19 } 20 Book b1=(Book)o1; 21 Book b2=(Book)o2; 22 //返回的正数还是负数,会影响排序的顺序 23 if(b1.getPrice()-b2.getPrice()>0){ 24 return 1; 25 }else if (b1.getPrice()-b2.getPrice()<0){ 26 return -1; 27 }else{ 28 return 0; 29 } 30 31 } 32 }); 33 34 System.out.println(Arrays.toString(books));*/ 35 36 Arrays.sort(books); 37 System.out.println(Arrays.toString(books)); 38 39 } 40 } 41 class Book implements Comparable{ 42 private String name; 43 private double price; 44 45 public Book(String name, double price) { 46 this.name = name; 47 this.price = price; 48 } 49 50 public String getName() { 51 return name; 52 } 53 54 public void setName(String name) { 55 this.name = name; 56 } 57 58 public double getPrice() { 59 return price; 60 } 61 62 public void setPrice(double price) { 63 this.price = price; 64 } 65 66 @Override 67 public String toString() { 68 return "Book[name="+name+",price="+price+"] "; 69 } 70 71 @Override 72 public int compareTo(Object o) { 73 if (!(o instanceof Book)){ 74 return 0; 75 } 76 Book book=(Book)o; 77 if (this.getPrice()>book.getPrice()){ 78 return 1; 79 }else if(this.getPrice()>this.getPrice()){ 80 return 1; 81 }else{ 82 return 0; 83 } 84 85 } 86 }
结果:
[Book[name=红楼梦,price=100.0] , Book[name=水浒传,price=90.0] , Book[name=青年文摘,price=5.0] , Book[name=java从入门到放弃,price=300.0] ]