• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Sorting using Comparable interface

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a collection (ArrayList) of objects obj1.
Each object in the collection inturn contains an ArrayList of objects obj2.
Now i want to sort the entire collection based on an attribute in obj2.
Any ideas on how to implement the Comparable inteface for the same?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's how to sort th eouter list based ona key in the inner list. Is that what you mean by sort the "entire collection"? You can implement Comparator to create an object that will be used to compare two objects in the outer list. That object can pull values out of the inner list to make the comparison. You pass your Comparator to the constructor of a TreeList. See if this makes sense:

The sort handed me two objects, which I know to be Lists. I get a pair of values out of the two lists and compare them.
Next, I see some duplicate code and probably make a getKey(Object) method to remove it.

Does that do what you need?
If you need sorted inner lists, repeat this process with a Comparator for inner TreeSets.
[ November 04, 2003: Message edited by: Stan James ]
 
Rajeshwari Natarajan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'll explain the requirement more in detail..
i have 2 objects Order and LineItem like this.

I will get a collection of Order objects and i have to dispaly the deails on screen.
The user can choose to sort the details based on orderNumber or productCode.
If he chooses productCode, then all the lineItems should be sorted. This sorting should be done considering the lineItems of all the orders(outer collection).
For example, consider that i have 2 order with orderNumber 5 and 3.
For the first order, let the lineItems have productCode 2 and 7.
For the second order, let the lineItems have productCode 3 and 5.
If i sort in ascending order based on productCode, i should get the data as,

How do achieve this?
Should i have 2 different compartor classes for the inner and outer collection?
[ November 04, 2003: Message edited by: Rajeshwari Natarajan ]
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can have one sort that will sort the orders, and another that will sort the line items, but that won't accomplish what you're trying to do. If you apply a sort to your collection of orders it can't intertwine the line items.
What you may have to do is to dump all of the line items from each order into a new [temporary] collection and sort that collection based on product code. Then print out the results. Of course you need to maintain the assocation back to the original order so that you can print out the order number. There are various ways of doing that.
 
Rajeshwari Natarajan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the solution Wayne. I'll try that out.
One way of maintaining association between orderNumber and productCode would be to have the orderNumber attribute in VO2 also. But that would make it reduntant data.
So possibly i should have a temporay Collection to store the association betwwn a lineItem and an orderNumber.
reply
    Bookmark Topic Watch Topic
  • New Topic