Hi ,
U can sort using comparator by having a flag to distinguish what to sort on
for eg if u want to sort  on product quantity
class Test implements comparable
{
  boolean flgSortOnProductPrice=false;
....
.
.
.
   public Test()
    {
    }
    public setFlgSortOnProductPrice( boolean sort)
    {
    this.flgSortOnProductPrice=true;
    //set all flags to false
    }
    public int compareTo(Object obj1,Object obj2)
    {
lineitem l1=(lineitem) obj1;
lineitem l2=(lineitem) obj2;
     i f(flgSortOnProductPrice)
          {
            return (l1.getProducttPrice()).compareTo(l2.getProductPrice())

        }
else
.
.
.
.
    }



}
now  oto use this
Arraylist al1;
test =new Test();
/say sort on product price
test.setFlgSortOnProductPrice(true);

Collections.sort(al1,test);


to sort on proctcode
test.setFlag(true);
Collections.sort(al1,test);
Maybe this helps
Regards,
Gaurav
Jerson Chua wrote:

> Hi...
> For example, if I have a ShoppingCart object which contains an ArrayList of
> lineItem objects. Each lineitem object contains the attribute productName,
> productPrice, productCode, and quantity.
>
> Here's the expected behavior.
>
> For example... initially it's sorted by
> productCode, productName, productPrice, and quantity.
>
> If the user clicks productName then it will be sorted by
> productName, productCode, productPrice, and quantity.
>
> If the user clicks productPrice then it will be sorted by
> productPrice, productName, productCode, and quantity.
>
> and so on...
>
> Initially I planned to use Comparator but this can only be used for sorting
> a single column.
>
> How do I implement such functionality?
>
> thank you very much your help will be very much appreciated.
>
> Jerson
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets




Jerson Chua wrote:

> Hi...
> For example, if I have a ShoppingCart object which contains an ArrayList of
> lineItem objects. Each lineitem object contains the attribute productName,
> productPrice, productCode, and quantity.
>
> Here's the expected behavior.
>
> For example... initially it's sorted by
> productCode, productName, productPrice, and quantity.
>
> If the user clicks productName then it will be sorted by
> productName, productCode, productPrice, and quantity.
>
> If the user clicks productPrice then it will be sorted by
> productPrice, productName, productCode, and quantity.
>
> and so on...
>
> Initially I planned to use Comparator but this can only be used for sorting
> a single column.
>
> How do I implement such functionality?
>
> thank you very much your help will be very much appreciated.
> Jerson
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to