java自定义排序
1 package GoodsSorting; 2 //实例类 3 public class GoodsParameter { 4 // 价格 5 private double price; 6 // 收藏量 7 private int favor; 8 // 商品名称 9 private String goodsname; 10 //无参构造 11 public GoodsParameter() { 12 super(); 13 } 14 //带参构造 15 public GoodsParameter(String goodsname, double price, int favor) { 16 super(); 17 this.price = price; 18 this.favor = favor; 19 this.goodsname = goodsname; 20 } 21 public double getPrice() { 22 return price; 23 } 24 public void setPrice(double price) { 25 this.price = price; 26 } 27 public int getFavor() { 28 return favor; 29 } 30 public void setFavor(int favor) { 31 this.favor = favor; 32 } 33 public String getGoodsname() { 34 return goodsname; 35 } 36 public void setGoodsname(String goodsname) { 37 this.goodsname = goodsname; 38 } 39 //重写tostring 40 @Override 41 public String toString() { 42 return "\t商品:" + getGoodsname() + "\t价格:" + getPrice() + "\t收藏量:" 43 + getFavor() + "\n"; 44 } 45 }
1 package GoodsSorting; 2 //排序工具类 3 import java.util.Comparator; 4 5 public class PriceSort implements Comparator<GoodsParameter> { 6 7 public int compare(GoodsParameter o1, GoodsParameter o2) { 8 int i=0; 9 //按价格排序 正序 10 if ((o1.getPrice() - o2.getPrice()) > 0) { 11 i=1; 12 13 } 14 else { 15 i=-1; 16 17 } 18 return i; 19 20 } 21 22 }
1 package GoodsSorting; 2 3 import java.util.Comparator; 4 5 public class FavorSort implements Comparator<GoodsParameter> { 6 7 public int compare(GoodsParameter o1, GoodsParameter o2) { 8 int i = 0; 9 // 按收藏排序 正序 10 if (o1.getFavor()-o2.getFavor()<0) { //逆序只需修改为>0 11 i=-1; 12 } 13 return i; 14 } 15 16 }
1 package GoodsSorting; 2 3 import java.util.Comparator; 4 5 public class NameSort implements Comparator<GoodsParameter> { 6 public int compare(GoodsParameter o1, GoodsParameter o2) { 7 int result = o1.getGoodsname().compareTo(o2.getGoodsname()); 8 return result; 9 } 10 11 }
1 package GoodsSorting; 2 //测试类 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 public class TestApp { 8 public static void main(String[] args) { 9 List<GoodsParameter> list = new ArrayList<GoodsParameter>(); 10 list.add(new GoodsParameter("iphone", 5200, 110000)); 11 list.add(new GoodsParameter("xiaomi", 1999, 50000)); 12 list.add(new GoodsParameter("huawei", 1899, 60054)); 13 list.add(new GoodsParameter("samsung", 4500, 80000)); 14 list.add(new GoodsParameter("meizu", 1699, 125863)); 15 // 排序前 16 System.out.println("原序:**********************"); 17 System.out.println(list); 18 19 Collections.sort(list, new PriceSort()); // 注意这里是collections 20 // 按价格排序后 21 System.out.println("价格排序:**********************"); 22 System.out.println(list); 23 // 收藏量正序 24 System.out.println("收藏量排序:**********************"); 25 Collections.sort(list, new FavorSort()); 26 System.out.println(list); 27 // 名称正序 28 System.out.println("名称排序:**********************"); 29 Collections.sort(list, new NameSort()); 30 System.out.println(list); 31 } 32 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。