Sorting collections based on nested collections

Discussion in 'Java' started by Doug Poland, Sep 27, 2003.

  1. Doug Poland

    Doug Poland Guest

    Hello,

    Is it possible to use a comparator, or other sorting technique, to sort
    a collection has fields that are themselves collections, and, use fields
    from the nested collection the sort criteria?

    For example, I have an ArrayList (POList) of Purchase Order objects
    (PO). Currently I use a comparator to sort based on fields of the
    objects in POList that are not collections i.e., String, Date, etc.

    However, one of the fields in POList is an ArrayList (LineItemsList) of
    Line Item Details objects (LineItems). There may be zero, one, or many
    LineItems objects in the POList.

    Let's say a PO has a getCustomerName field and a LineItems has a
    getDeliveryDate field. I'd like to sort the POList based on
    getCustomerName from the PO object and getDeliveryDate from the many
    LineItems objects.

    I've been googling and have turned up nothing so far. Thanks for your help!

    --
    Regards,
    Doug
    Doug Poland, Sep 27, 2003
    #1
    1. Advertising

  2. Doug Poland

    VisionSet Guest

    "Doug Poland" <> wrote in message
    news:GAgdb.30581$-kc.rr.com...
    >
    > For example, I have an ArrayList (POList) of Purchase Order objects
    > (PO). Currently I use a comparator to sort based on fields of the
    > objects in POList that are not collections i.e., String, Date, etc.
    >
    > However, one of the fields in POList is an ArrayList (LineItemsList) of
    > Line Item Details objects (LineItems). There may be zero, one, or many
    > LineItems objects in the POList.


    Are you saying that POList extends ArrayList and collects PO objects, but
    also has another ArrayList as an attribute, and this collects LineItem
    objects?

    ie

    class POList extends ArrayList {

    ArrayList myLineItemObjects;
    }

    What do you mean by field?

    --
    Mike W
    VisionSet, Sep 27, 2003
    #2
    1. Advertising

  3. Doug Poland

    Doug Poland Guest

    VisionSet wrote:

    > "Doug Poland" <> wrote in message
    > news:GAgdb.30581$-kc.rr.com...
    >
    >>For example, I have an ArrayList (POList) of Purchase Order objects
    >>(PO). Currently I use a comparator to sort based on fields of the
    >>objects in POList that are not collections i.e., String, Date, etc.
    >>
    >>However, one of the fields in POList is an ArrayList (LineItemsList) of
    >>Line Item Details objects (LineItems). There may be zero, one, or many
    >>LineItems objects in the POList.

    >
    >
    > Are you saying that POList extends ArrayList and collects PO objects, but
    > also has another ArrayList as an attribute, and this collects LineItem
    > objects?
    >
    > ie
    >
    > class POList extends ArrayList {
    >
    > ArrayList myLineItemObjects;
    > }
    >
    > What do you mean by field?
    >

    Hope I can explain it adequately... POList is implemented as a simple
    ArrayList, it's not a class. POList's members are PO objects. Each PO
    object has attributes or fields (I think that's the proper terminology
    from a JavaBean perspective). So a PO has a getter and setter for
    customerName, poNumber, poDate, etc. One of the PO fields/attributes is
    lineItems of type List. It is implemented as an ArrayList. Each item
    in this ArrayList is a LineItems object, with getters/setters of it's
    own. Does that make sense?

    --
    Regards,
    Doug
    Doug Poland, Sep 27, 2003
    #3
  4. Doug Poland

    VisionSet Guest

    "Doug Poland" <> wrote in message
    news:...
    >
    > Hope I can explain it adequately... POList is implemented as a simple
    > ArrayList, it's not a class. POList's members are PO objects. Each PO
    > object has attributes or fields (I think that's the proper terminology
    > from a JavaBean perspective). So a PO has a getter and setter for
    > customerName, poNumber, poDate, etc. One of the PO fields/attributes is
    > lineItems of type List. It is implemented as an ArrayList. Each item
    > in this ArrayList is a LineItems object, with getters/setters of it's
    > own. Does that make sense?
    >


    Got you, so you want it ordered like this:

    Customer: Alpha
    LineItem: Early Date
    LineItem: Late Date
    Customer: Beta
    LineItem: Early Date
    LineItem: Late Date

    No hang on you want to order POList by LineItem date & CustomerName.
    So order by customer name 1st then LineItem date ie equal customer names are
    then ordered by LineItem date?
    But which LineItem?, an average the 1st...

    --
    Mike W
    VisionSet, Sep 27, 2003
    #4
  5. Doug Poland

    Doug Poland Guest

    VisionSet wrote:

    >
    > Got you, so you want it ordered like this:
    >
    > Customer: Alpha
    > LineItem: Early Date
    > LineItem: Late Date
    > Customer: Beta
    > LineItem: Early Date
    > LineItem: Late Date
    >
    > No hang on you want to order POList by LineItem date & CustomerName.
    > So order by customer name 1st then LineItem date ie equal customer

    names are
    > then ordered by LineItem date?
    > But which LineItem?, an average the 1st...
    >
    >

    Close :) I'll modify your example...


    Customer: Alpha
    LineItem01: Quantity=20, Status=Delivered, Date=09/21/2003
    LineItem02: Quantity=40, Status=Shipped, Date=09/23/2003
    Customer: Beta
    LineItem01: Quantity=60, Status=Cancelled, Date=09/24/2003
    Customer: Charlie
    LineItem01: Quantity=5, Status=Delivered, Date=09/05/2003
    LineItem02: Quantity=5, Status=Shipped, Date=09/07/2003
    LineItem03: Quantity=5, Status=Shipped, Date=09/09/2003

    So if we were to flatten this like a resultset from a relational join...

    Customer LineItem Quantity Status Date
    -------- -------- -------- --------- ----------
    Alpha 01 20 Delivered 09/21/2003
    Alpha 02 40 Shipped 09/23/2003
    Beta 01 60 Cancelled 09/24/2003
    Charlie 01 5 Delivered 09/05/2003
    Charlie 02 5 Shipped 09/07/2003
    Charlie 03 5 Shipped 09/09/2003

    select * from po,lines where ... order by status, customer, date desc

    Which would look like this...

    Customer LineItem Quantity Status Date
    -------- -------- -------- --------- ----------
    Beta 01 60 Cancelled 09/24/2003
    Alpha 01 20 Delivered 09/21/2003
    Charlie 01 5 Delivered 09/05/2003
    Alpha 02 40 Shipped 09/23/2003
    Charlie 03 5 Shipped 09/09/2003
    Charlie 02 5 Shipped 09/07/2003

    I apologize for not having workable examples to being with.
    Thanks for your help so far.

    --
    Regards,
    Doug
    Doug Poland, Sep 27, 2003
    #5
  6. Doug Poland

    Roedy Green Guest

    On Sat, 27 Sep 2003 13:50:30 GMT, Doug Poland <>
    wrote or quoted :

    >Is it possible to use a comparator, or other sorting technique, to sort
    >a collection has fields that are themselves collections, and, use fields
    >from the nested collection the sort criteria?


    You obviously need to write an odd Comparator that fishes the fields
    out.

    Since a sort often looks at each key more than once, it may pay to
    extract the necessary info from the subcollections before sorting.




    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 27, 2003
    #6
  7. Doug Poland

    Roedy Green Guest

    On Sat, 27 Sep 2003 13:50:30 GMT, Doug Poland <>
    wrote or quoted :

    > I'd like to sort the POList based on
    >getCustomerName from the PO object and getDeliveryDate from the many
    >LineItems objects.


    You might like to maintain the delivery date for sort ( e.g. last,
    first, lowest, highest ) as a separate field.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 27, 2003
    #7
  8. Doug Poland

    VisionSet Guest

    "Doug Poland" <> wrote in message
    news:...
    >
    >
    > Customer: Alpha
    > LineItem01: Quantity=20, Status=Delivered, Date=09/21/2003
    > LineItem02: Quantity=40, Status=Shipped, Date=09/23/2003
    > Customer: Beta
    > LineItem01: Quantity=60, Status=Cancelled, Date=09/24/2003
    > Customer: Charlie
    > LineItem01: Quantity=5, Status=Delivered, Date=09/05/2003
    > LineItem02: Quantity=5, Status=Shipped, Date=09/07/2003
    > LineItem03: Quantity=5, Status=Shipped, Date=09/09/2003
    >
    > So if we were to flatten this like a resultset from a relational join...
    >
    > Customer LineItem Quantity Status Date
    > -------- -------- -------- --------- ----------
    > Alpha 01 20 Delivered 09/21/2003
    > Alpha 02 40 Shipped 09/23/2003
    > Beta 01 60 Cancelled 09/24/2003
    > Charlie 01 5 Delivered 09/05/2003
    > Charlie 02 5 Shipped 09/07/2003
    > Charlie 03 5 Shipped 09/09/2003
    >
    > select * from po,lines where ... order by status, customer, date desc
    >
    > Which would look like this...
    >
    > Customer LineItem Quantity Status Date
    > -------- -------- -------- --------- ----------
    > Beta 01 60 Cancelled 09/24/2003
    > Alpha 01 20 Delivered 09/21/2003
    > Charlie 01 5 Delivered 09/05/2003
    > Alpha 02 40 Shipped 09/23/2003
    > Charlie 03 5 Shipped 09/09/2003
    > Charlie 02 5 Shipped 09/07/2003
    >


    Well won't just running a sort against a relevent comparator do the trick?
    Sort both array lists, and you should have what you want.

    --
    Mike W
    VisionSet, Sep 27, 2003
    #8
  9. Doug Poland

    Doug Poland Guest

    VisionSet wrote:
    >>
    >>select * from po,lines where ... order by status, customer, date desc
    >>
    >>Which would look like this...
    >>
    >>Customer LineItem Quantity Status Date
    >>-------- -------- -------- --------- ----------
    >>Beta 01 60 Cancelled 09/24/2003
    >>Alpha 01 20 Delivered 09/21/2003
    >>Charlie 01 5 Delivered 09/05/2003
    >>Alpha 02 40 Shipped 09/23/2003
    >>Charlie 03 5 Shipped 09/09/2003
    >>Charlie 02 5 Shipped 09/07/2003
    >>

    >
    > Well won't just running a sort against a relevent comparator do
    > the trick? Sort both array lists, and you should have what you want.
    >

    That's what I'm hoping, but I can't figure out how a comparator would
    work on the nested collection.

    For example, here's a comparator that would sort on POList
    Customer Name...


    public int compare(Object arg0, Object arg1) {

    PurchaseOrders po1 = (PurchaseOrders) arg0;
    PurchaseOrders po2 = (PurchaseOrders) arg1;

    //
    // This compare method sorts on Name
    //
    return po1.getCustomerName().compareTo(po2.getCustomerName());

    }


    So how does one sort the nested collection?

    public int compare(Object arg0, Object arg1) {

    PurchaseOrders po1 = (PurchaseOrders) arg0;
    PurchaseOrders po2 = (PurchaseOrders) arg1;
    int result = 0;

    //
    // This compare method sorts on Status ASC, Date DESC, Name ASC
    //
    Collections.sort(po1.getItemsList(), new ComparePO_Status());
    Collections.sort(po2.getItemsList(), new ComparePO_Status());

    /* ????
    *
    * So how do I return an int from this?
    * Each sublist is sorted on status, how do I tell the
    * comparator to return a -1, 0, or 1?
    */

    return result;
    }


    --
    Regards,
    Doug
    Doug Poland, Sep 27, 2003
    #9
  10. Doug Poland

    VisionSet Guest

    "Doug Poland" <> wrote in message
    news:...
    > VisionSet wrote:
    > >>
    > >>select * from po,lines where ... order by status, customer, date desc
    > >>
    > >>Which would look like this...
    > >>
    > >>Customer LineItem Quantity Status Date
    > >>-------- -------- -------- --------- ----------
    > >>Beta 01 60 Cancelled 09/24/2003
    > >>Alpha 01 20 Delivered 09/21/2003
    > >>Charlie 01 5 Delivered 09/05/2003
    > >>Alpha 02 40 Shipped 09/23/2003
    > >>Charlie 03 5 Shipped 09/09/2003
    > >>Charlie 02 5 Shipped 09/07/2003
    > >>

    > >
    > > Well won't just running a sort against a relevent comparator do
    > > the trick? Sort both array lists, and you should have what you want.
    > >

    > That's what I'm hoping, but I can't figure out how a comparator would
    > work on the nested collection.
    >
    > For example, here's a comparator that would sort on POList
    > Customer Name...
    >
    >
    > public int compare(Object arg0, Object arg1) {
    >
    > PurchaseOrders po1 = (PurchaseOrders) arg0;
    > PurchaseOrders po2 = (PurchaseOrders) arg1;
    >
    > //
    > // This compare method sorts on Name
    > //
    > return po1.getCustomerName().compareTo(po2.getCustomerName());
    >
    > }
    >
    >
    > So how does one sort the nested collection?
    >
    > public int compare(Object arg0, Object arg1) {
    >
    > PurchaseOrders po1 = (PurchaseOrders) arg0;
    > PurchaseOrders po2 = (PurchaseOrders) arg1;
    > int result = 0;
    >
    > //
    > // This compare method sorts on Status ASC, Date DESC, Name ASC
    > //
    > Collections.sort(po1.getItemsList(), new ComparePO_Status());
    > Collections.sort(po2.getItemsList(), new ComparePO_Status());
    >
    > /* ????
    > *
    > * So how do I return an int from this?
    > * Each sublist is sorted on status, how do I tell the
    > * comparator to return a -1, 0, or 1?
    > */
    >
    > return result;
    > }
    >


    Since you've got this bit:

    return po1.getCustomerName().compareTo(po2.getCustomerName());

    then obviously the LineItem version is going to be similar.

    So just sort it in the same way, the only thing really to decide is when &
    where you do it.

    Perhaps on addition to the PurchaseOrder? or whenever you've stopped making
    additions to the LineItems collection.

    ie

    class PurchaseOrder {

    addLineItems(List items) {
    lineItems = items;
    Collections.sort(lineItems, new LineItemComparator());
    }
    }


    Sorry, I fail to see any real problem.

    --
    Mike W
    VisionSet, Sep 27, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn Zavoda
    Replies:
    1
    Views:
    354
    Shawn Zavoda
    Mar 5, 2004
  2. Prakash C via .NET 247

    Problem with Nested Collections

    Prakash C via .NET 247, Sep 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    376
    avnrao
    Sep 2, 2004
  3. John
    Replies:
    4
    Views:
    914
    RedGrittyBrick
    Apr 1, 2008
  4. petek1976

    Java VM 1.6.0 Sorting Collections

    petek1976, Jun 10, 2010, in forum: Java
    Replies:
    28
    Views:
    1,875
    ClassCastException
    Jun 11, 2010
  5. mutex
    Replies:
    0
    Views:
    212
    mutex
    Jul 27, 2003
Loading...

Share This Page