aspose file tools*
The moose likes Java in General and the fly likes Sorting using Comparable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting using Comparable interface" Watch "Sorting using Comparable interface" New topic
Author

Sorting using Comparable interface

Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
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?


regards<br />Rajeshwari. N
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rajeshwari Natarajan
Ranch Hand

Joined: Mar 05, 2003
Posts: 67
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 ]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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

Joined: Mar 05, 2003
Posts: 67
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting using Comparable interface
 
Similar Threads
Jdiscuss question of the day - April 3, 2005
ArrayList class
Unit Test compareTo / Comparable
when and how is compareTo() called for each element
Remove a subset of objects at once