手机按价格排序
package cd_one.code14.exer4;
public class Product implements Comparable{
String name;
double price;
public Product() {
}
public Product(String name, double price) {
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 "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
@Override
public int compareTo(Object o) {
if(o == this){
return 0;
}
if(o instanceof Product){
Product p = (Product) o;
return Double.compare(this.price,p.price);
}
throw new RuntimeException("类型不匹配");
}
}
package cd_one.code14.exer4;
import org.junit.Test;
import java.util.Arrays;
public class ComparableTest {
@Test
public void test(){
String[] arr = new String[]{"Tom","Jerry","Rose","Jack",
"Lucy"};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test1(){
Product[] arr = new Product[5];
arr[0] = new Product("HuaweiMate50pro",6999);
arr[1] = new Product("Xiaomi13pro",4999);
arr[2] = new Product("VivoX90pro",5999);
arr[3] = new Product("Iphone14ProMax",9999);
arr[4] = new Product("HonorMagic4",6299);
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Product{name='Xiaomi13pro', price=4999.0}
Product{name='VivoX90pro', price=5999.0}
Product{name='HonorMagic4', price=6299.0}
Product{name='HuaweiMate50pro', price=6999.0}
Product{name='Iphone14ProMax', price=9999.0}
第二种从大到小
@Override
public int compareTo(Object o) {
if(o == this){
return 0;
}
if(o instanceof Product){
Product p = (Product) o;
int value = Double.compare(this.price,p.price);
if(value != 0){
return -value;
}
return this.name.compareTo(p.name);
}
throw new RuntimeException("类型不匹配");
}
价格大到小
Product{name='Iphone14ProMax', price=9999.0}
Product{name='HuaweiMate50pro', price=6999.0}
Product{name='HonorMagic4', price=6299.0}
Product{name='VivoX90pro', price=5999.0}
Product{name='Xiaomi13pro', price=4999.0}
按名字来排:
Product{name='Xiaomi13pro', price=4999.0}
Product{name='VivoX90pro', price=5999.0}
Product{name='HonorMagic4', price=6299.0}
Product{name='HuaweiMate50pro', price=6999.0}
Product{name='Iphone14ProMax', price=9999.0}
方法二:
package cd_one.code14.exer4;
public class Product implements Comparable{
String name;
double price;
public Product() {
}
public Product(String name, double price) {
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 "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
@Override
public int compareTo(Object o) {
if(o == this){
return 0;
}
if(o instanceof Product){
Product p = (Product) o;
int value = Double.compare(this.price,p.price);
if(value != 0){
return value;
}
return this.name.compareTo(p.name);
}
throw new RuntimeException("类型不匹配");
}
}
package cd_one.code14.exer4;
import org.junit.Test;
import java.util.Arrays;
public class ComparableTest {
@Test
public void test(){
String[] arr = new String[]{"Tom","Jerry","Rose","Jack",
"Lucy"};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test1(){
Product[] arr = new Product[5];
arr[0] = new Product("HuaweiMate50pro",6999);
arr[1] = new Product("Xiaomi13pro",4999);
arr[2] = new Product("VivoX90pro",5999);
arr[3] = new Product("Iphone14ProMax",9999);
arr[4] = new Product("HonorMagic4",6299);
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test2(){
Product p1 = new Product("HuaweiMate50pro",6999);
Product p2 = new Product("Xiaomi13pro",4999);
int compare = p1.compareTo(p2);
if(compare > 0){
System.out.println("p1大");
}else if(compare < 0) {
System.out.println("p2大");
}else{
System.out.println("p1和p2一样大");
}
}
}
p1大
package cd_one.code14.exer4;
import org.junit.Test;
import java.util.Arrays;
import java.util.Comparator;
public class ComparatorTest {
@Test
public void test(){
Product[] arr = new Product[5];
arr[0] = new Product("HuaweiMate50pro",6999);
arr[1] = new Product("Xiaomi13pro",4999);
arr[2] = new Product("VivoX90pro",5999);
arr[3] = new Product("Iphone14ProMax",9999);
arr[4] = new Product("HonorMagic4",6299);
Comparator comparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Product && o2 instanceof Product){
Product p1 = (Product) o1;
Product p2 = (Product) o2;
return -Double.compare(p1.getPrice(),p2.getPrice());
}
throw new RuntimeException("类型不匹配");//如果这个东西不是一个产品则抛出异常
}
};
Arrays.sort(arr,comparator);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test1(){
Product[] arr = new Product[5];
arr[0] = new Product("HuaweiMate50pro",6999);
arr[1] = new Product("Xiaomi13pro",4999);
arr[2] = new Product("VivoX90pro",5999);
arr[3] = new Product("Iphone14ProMax",9999);
arr[4] = new Product("HonorMagic4",6299);
Comparator comparator1 = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Product && o2 instanceof Product){
Product p1 = (Product) o1;
Product p2 = (Product) o2;
return -Double.compare(p1.getPrice(),p2.getPrice());
}
throw new RuntimeException("类型不匹配");//如果这个东西不是一个产品则抛出异常
}
};
Arrays.sort(arr,comparator1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test2(){
String[] arr = new String[]{"Tom","Jerry","Tony","Rose","Jack","Lucy"};
Arrays.sort(arr, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1 instanceof String && o2 instanceof String){
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
throw new RuntimeException("类型不匹配");
}
});
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
@Test
public void test3(){
Product[] arr = new Product[5];
arr[0] = new Product("HuaweiMate50pro",6999);
arr[1] = new Product("Xiaomi13pro",4999);
arr[2] = new Product("VivoX90pro",5999);
arr[3] = new Product("Iphone14ProMax",9999);
arr[4] = new Product("HonorMagic4",6299);
Arrays.sort(arr, new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
if(o1 instanceof Product && o2 instanceof Product){
Product p1 = (Product) o1;
Product p2 = (Product) o2;
return Double.compare(p1.getPrice(),p2.getPrice());
}
throw new RuntimeException("类型不匹配");
}
});
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}