【问题标题】:Sorting and secondary sorting in java using Coamparable interfacejava中使用Coamparable接口进行排序和二次排序
【发布时间】:2015-04-10 16:18:07
【问题描述】:

我有一个名为 Order 的类,其中我有 OrderIDprodustID 字段的 getter 和 setter 方法。

1) 我想以升序排序 orderID 并以降序排序对应的 productID。所以我使用 Comparable interfacecompareTo 方法来做到这一点。我能够对 orderID 进行排序,但相应的 productID 没有按降序排序(二次排序)。

请告诉我该怎么做。

我用过的代码:

   @Override
public int compareTo(Order ord)
{
    double orderId1 = ((Order) ord).getOrderId();
    return (int) (this.orderId - orderId1);
    //return orderId.equals(ord.orderId);
}

2)我想根据orderID进行搜索,想得到对应的productid

这里我使用的是硬编码数据。

我的数据是这样的:

订单数据:

orderID  productID
1001        22
1003        33
1001         33

排序所需的输出是:

 orderID  productID
1001        33
1001        22
1003        33

【问题讨论】:

  • 显示您使用比较器对集合进行排序的代码。

标签: java sorting comparable


【解决方案1】:

我认为这应该可行:

public int compareTo(Order ord)
{
    if (this.orderId == ord.getOrderId()){
       return (int) (ord.getProductId() - this.productId);
    }
    return  this.orderId - ord.getOrderId();
}

如果只有当 orderId 等于时才比较 productId。

【讨论】:

    【解决方案2】:

    最好的方法是使用内置函数进行比较。

    @Override
    public int compareTo(Order o) {
        int d = Double.compare(this.getOrderId(), o.getOrderId());
        if (d == 0) {
            d = Double.compare(o.getProductId(), this.getProductId());
        }
        return d;
    }
    

    但我真的不认为你的orderIdproductId 应该是double 类型。最好将它们设为int。如果你改成int,那么还是使用内置的比较器,改成Integer.compare(x,y)就行了。

    【讨论】:

    • 嘿,非常感谢。排序和二次排序工作正常。但是如何根据 orderId 进行搜索并获取对应的 productID。在这里,如果我创建名为 SearchByOrderid() 的方法,那么当我将 orderid 作为 1001 传递时,我应该将所有 prodid 附加到该方法上。我该怎么做。
    • @shree11 这取决于您使用的集合类型。你能用这些信息更新你的问题吗?
    • 这里我使用List<Order> OrderList= new ArrayList<Order>();。这里创建了名为 SearchByOrderID(Order id); 的方法因此,每当我传递 OrderID 时,我都应该将所有 productID 都附加到它上面。
    • 我可以这样做吗:public Order1 SearchByOrderID(int o) { //logic for search } 和主方法调用 SerachByOrderID。但我不知道该怎么做
    【解决方案3】:

    你可以试试这个:

    @Override
    public int compareTo( Order ord )
    {
        if( this.orderId != ord.getOrderId() )
        {
            return Double.compare( this.orderId, ord.getOrderId() );
        }
        return Double.compare( ord.getProductId(), this.productId );
    }
    

    【讨论】:

      【解决方案4】:

      这是一个包含您订购商品的数据的示例

      package test;
      
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.List;
      
      public class TestOrder {
      
          public static void main(String[] args) {
              List<OrderProduct> myList=new ArrayList<>();
              myList.add(new OrderProduct(22, 1001));
              myList.add(new OrderProduct(33, 1003));
              myList.add(new OrderProduct(33, 1001));
              Collections.sort(myList);
              for (OrderProduct op : myList) {
                  System.out.println(op.getOrderId() + " " + op.productId);
              }
      
          }
      
          private static class OrderProduct implements Comparable<OrderProduct>{
              private int productId;
              private int orderId;
      
              public OrderProduct(int productId, int orderId) {
                  super();
                  this.productId = productId;
                  this.orderId = orderId;
              }
      
      
              public int getProductId() {
                  return productId;
              }
              public int getOrderId() {
                  return orderId;
              }
      
      
              @Override
              public int compareTo(OrderProduct o) {
                  if (o==null) {
                      return 1;
                  }
                  if (orderId==o.orderId) {
                      return o.productId-productId;
                  }
                  return orderId-o.orderId;
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-29
        • 2017-02-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-23
        • 2013-04-30
        • 2013-04-22
        • 1970-01-01
        相关资源
        最近更新 更多