1

I have class called Order where I have getter and setter methods for OrderID and produstID field.

1) I want to get sort orderID in asending order and corresponding productID in desending order. So I used Comparable interface and compareTo methods to do that.I'm able to sort orderID but corresponding productID's are not sorted in descending order(secondary sorting).

Please let me know how can i do that.

Code i have used :

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

2) I wanted to search based on orderID and want to get corresponding productid's.

Here I'm using hard coded data.

My data lokks like this:

Order data:

orderID  productID
1001        22
1003        33
1001         33

Desired output for soring is :

 orderID  productID
1001        33
1001        22
1003        33
1
  • Show the code where you use the comparator to sort the collection. Commented Apr 10, 2015 at 6:22

4 Answers 4

3

I think this should work:

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

if have to compare the productId only if the is orderId equals.

Sign up to request clarification or add additional context in comments.

Comments

1

Best way is to use built-in functions for comparing.

@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;
}

But I don't really think that your orderId and productId should be of double type. Better have them as int. If you change to int then still use the built-in comparators, just change to Integer.compare(x,y).

7 Comments

Hey thank you so much. Sorting and secondary sorting is working fine. But how can i search based on orderId and get corresponding productID. Here if i create method called SearchByOrderid(), then when i pass orderid as 1001, then i should get all the prodid's attached to that. How can i do this.
@shree11 That depends on what kind of collection you are using. Can you update your question with that information?
Here i'm using List<Order> OrderList= new ArrayList<Order>();. Here have created method called SearchByOrderID(Order id); So whenever i pass the OrderID, i should get all the productID's attached to it.
@shree11 I think you'll need to use a TreeMap instead and then you can use TreeMap#subMap to get a partial view of the TreeMap with all the objects you are interrested in.
Can i do like this : public Order1 SearchByOrderID(int o) { //logic for search } and main method call SerachByOrderID. But i do not know how to do it
|
0

You could try this:

@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 );
}

Comments

0

Here is a sample with your data that orders the items

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;
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.