Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList sorting

 
Tony Fabeets
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Tony Fabeets
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
figured it out. used Collections.sort(ArrayList) did a compareTo(Object) containing tests that returned -1,0,1
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great. Hope we were of some help here.
 
Tony Fabeets
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
didn't see how obvious it was til i posted.
if(var1 == other.var1)
if(var2 > other.var2)return -1;
etc...
thx all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic