zoukankan      html  css  js  c++  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     //重写tostring
    40     @Override
    41     public String toString() {
    42         return "	商品:" + getGoodsname() + "	价格:" + getPrice() + "	收藏量:"
    43                 + getFavor() + "
    ";
    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 }
  • 相关阅读:
    PYFLINK 基础 (一):运行相关(一)PYFLINK安装与本地运行(WINDOWS10)(TABLE demo)
    linux 开机进入initramfs无法开机
    VBA删除 语法
    VBA 上传数据与查找数据 while循环 和 for循环
    odoo tree视图 当页不弹窗显示方法
    VBA 连接,提醒 rs AS new adodb.recordset 的变量未定义
    odoo 订单打印 会出现字体. ........... 虚线问题
    270. Closest Binary Search Tree Value
    277. Find the Celebrity
    724. Find Pivot Index
  • 原文地址:https://www.cnblogs.com/Ouyangan/p/4132903.html
Copyright © 2011-2022 走看看