import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class TestPaixu {
public static void main(String[] args) {
Apple1 a1 = new Apple1(0,"胖子",41.33,100.41);
Apple1 a2 = new Apple1(1,"船长",20.848,100.5);
Apple1 a3 = new Apple1(2,"sb",10.7,100.41);
Apple1 a4 = new Apple1(1,"颜师傅",40,87);
Apple1 a5 = new Apple1(3,"马神",5.12,100.41);
Apple1 a6 = new Apple1(0,"门卫",22.08,100.5);
Apple1 a7 = new Apple1(3,"马神2",5.11,100.41);
Apple1 a8 = new Apple1(0,"门外2",22.18,100.5);
List<Apple1> list = new ArrayList<>(); //创建集合,把new的对象
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
list.add(a5);
list.add(a6);
list.add(a7);
list.add(a8);
list.sort(new Comparator<Apple1>() { //排升序 return -()排降序
public int compare(Apple1 o1, Apple1 o2) {
String str1 = String.valueOf(o1.getAmount()); //将数量amount转成字符串
String str2 = String.valueOf(o2.getAmount());
String str11 =str1.substring(str1.indexOf(".")+1, str1.length()); //截取字符串,截取出小数点后面的数字
String str22 =str2.substring(str2.indexOf(".")+1, str2.length());
int o1substart = Integer.parseInt(str1.substring(0,str1.indexOf("."))); //截取出小数点前的数字
int o2substart = Integer.parseInt(str2.substring(0,str2.indexOf(".")));
int o1subend = Integer.parseInt(str11);//小数点后的数字
int o2subend = Integer.parseInt(str22);
if((o1.getId()-o2.getId())==0&&(o1substart-o2substart)!=0){ //先判断两个对象的id,如果id相等并且小数点前的数字不相等,
//那么就按照小数点前的数字排序
return (o1substart-o2substart)*-1;
}else if((o1.getId()-o2.getId())==0&&(o1substart-o2substart)==0){ //如果id相等,且小数点前的数字也相等
return (o1subend-o2subend)*-1; //那么就按照小数点后的数字排序
}
else{ //如果两个对象的id不相等,直接按照id排序
return (o1.getId()-o2.getId())*-1;
}
}
});
for(Apple1 x:list){
System.out.println(x);
}
}
}
class Apple1 implements Serializable{
private int id;
private String name;
private double amount;
private double weight;
public Apple1() {
}
public Apple1(int id, String name, double amount, double weight) {
this.id = id;
this.name = name;
this.amount = amount;
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple1 [id=" + id + ", name=" + name + ", amount=" + amount + ", weight=" + weight + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(amount);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
temp = Double.doubleToLongBits(weight);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Apple1 other = (Apple1) obj;
if (Double.doubleToLongBits(amount) != Double.doubleToLongBits(other.amount))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(weight) != Double.doubleToLongBits(other.weight))
return false;
return true;
}
}