首页 > 代码库 > java自定义排序

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     //重写tostring40     @Override41     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) {  //逆序只需修改为>011             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()); // 注意这里是collections20         // 按价格排序后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 }

 

java自定义排序