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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList sorting" Watch "ArrayList sorting" New topic
Author

ArrayList sorting

Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
2 arrayLists have been created. addAL using the addClass constructor and shipAL using the shipClass constructor. I pass the arrays over to a sorter method that resides in an independant class. I would like to override the compareTo() method in the addClass and shipClass to sort the 2 arrays by a particular variable, itmNum, from inside the sorter method. Any ideas?
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Create an abstract super class for both addClass and shipClass that implements Comparable and define the compareTo() method in the abstract class. This assumes that you are sorting on a field (or group of fields) that is common to both classes. Then, all you need to do is call:

Both arrays will now be sorted. If you want their natarual sorting order to be independent then make each class a Comparble and define the compareTo() method in each. Either way there is no need to use an independent class to do the sorting when you already have java.util.Collections.sort(List).


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
the add array of objects are to be sorted by var1, while ship array are to be sorted by var2. Been reading over the api and just can't seem to get what goes in my overriding compareTo() in each class
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Assuming that var1 is an int:

Do the same for shipClass except use var2 and test for shipClass. If the tested variable is a floating point be careful for rounding errors, you will then need to use an if statement something like:
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[MM]: If the tested variable is a floating point be careful for rounding errors, you will then need to use an if statement something like:
Actually the code shown in Michael's first code block is risky if the tested variable is an integer as well, because there's the possibiliity of overflow. E.g. if you compare 2000000000 to -2000000000, you might get the result that -2000000000 is greater - because (2000000000) - (-2000000000) evaluates to -294967296, using int variables. If you're not 100% sure that the values you're using will make this type of overflow impossible, you should be safe and write the comparison test to use comparison operators like < or > rather than subtraction, as shown in Michael's last code block.
[ December 28, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
[JY] Actually the code shown in Micahel's first code block is risky if the tested variable is an integer as well, because there's the possibiliity of overflow.
Picky, picky, picky But as always you're right.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Except the "Micahel" part of course. But you've been gone so long; I have a lot of nitpicking to catch up on.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Except the "Micahel"
That's pretty close to the Latin which is how my priest adresses me.
But you've been gone so long
I've been busier than a one-legged cat coverin' up poop on ice. I am doing OJT (On job training) and studying the framework and infrastructure at USAA and learning the intricacies of WSAD. It seems like I am taking more courses at work than I used to at college. The good news is I get every other Friday off so I try to catch up at JavaRanch then.
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
figured it out. used Collections.sort(ArrayList) did a compareTo(Object) containing tests that returned -1,0,1
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Great. Hope we were of some help here.
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
yes, another instance of help has occured
a new issue has arised in the meantime. I am now able to sort the lists accordingly, but if there are instances where var2 is the same in multiple objects, i then need to sort those objects by var3.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Where did var2 come from? I thought it was var1. Well anyway, here's a technique for comparing by var1, then var2, then var3...

BTW this is a good excuse to recommend Effective Java by Joshua Bloch, which covers best practices for implementing Comparable on pp. 53-57. The other pages are about other stuff, but also extremely useful. This is probably the one book that every Java developer (past beginner) should have, in my opinion.
[ December 29, 2003: Message edited by: Jim Yingst ]
Tony Fabeets
Ranch Hand

Joined: Dec 22, 2003
Posts: 38
didn't see how obvious it was til i posted.
if(var1 == other.var1)
if(var2 > other.var2)return -1;
etc...
thx all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList sorting