File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Trying to compare Objects in multiple ways Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to compare Objects in multiple ways" Watch "Trying to compare Objects in multiple ways" New topic

Trying to compare Objects in multiple ways

Johny Doey

Joined: Feb 17, 2007
Posts: 7

basically I have two classes. One is Person and one is PersonArray. Person has the fields uID, fName, lName
PersonArray just saves them in an array called pArray. It has other functions, but those aren't important for this.

I implemented the Comparable interface in the Person class as such:
Now this works just fine (if you have suggestions on making it more efficient shoot) and when sorting I use Arrays.sort(pArray) (inside of a try ctach NullPointerException since the array may not be full, but I choose to ignore the exception) method.
Now I also want to be able to sort via uID, they are always unique so that's not an issue, but how do I do that? My first thought was to just create a "boolean switch" in the Person class but that means I have to flip that switch in every instance. I thought that sucks, tried it anyways. Didn't work, at least not properly. It sort of sorts the array in halves... and then once I've done that everything breaks. No more sorting will work at all

Now I've played with the code while writing and I still have that same code after having removed the integer crap that doesn't work and now nothing works at all anymore.

I figured out why. If my array is too small the sorting messes up. Get this I have an array of size 25 and 12 entries, the sort gives bad results, if the size is 50 it works again. then my "boolean switch" sorting works as well... but there has to be a better way, obviously I am doing something wrong, but what? Should I compare the strings differently?

Another issue I have is that when I add a Person to the array, then print out the array I can see it's been added, but if I then try to sort it gives me the ClassCastException, but I don't know why, because I am passing the right class. If I comment out that exception check and then try to sort it just ignores those entries. they are still in the array though. I can retrieve them with my getPerson(int i) method.

What did I break?

And if anything is unclear or you need more info, just shout.
[ February 17, 2007: Message edited by: Johny Doey ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Generally the way I approach sorting a group of name is creating a small private method within the class containing the names called key(). Then I make the key of that class be the last name followed by the first name. That is generally the way you would sort names in a phone book because the last name should be considered first, and then if they are the same, consider the first name.

As for sorting by uid, you could create a seperate Comparator class.

If you're getting a ClassCastException, are you sure that you list implements Comparable in the class header?
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

My first thought was to just create a "boolean switch"

You need to subclass compare functionalities by implementing Comparator instead. That way you have a NameComparator and a UIDComparator. Now use one of the overloaded Arrays.sort() methods that takes a Comparator as an argument to achieve what you want.

inside of a try catch NullPointerException

Hmm... go through the Arrays class documentation. Remove null references or write the compare code in a better way.

but if I then try to sort it gives me the ClassCastException

A Book "is not a" Person. So it throws the ClassCastException like you coded. A ClassCastException is usually thrown by the JVM not a programmer. You might want to rethink throwing it in your application logic.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Johny Doey

Joined: Feb 17, 2007
Posts: 7
I'd like to say thanks to Keith Lynn, but I don't really feel it is necessary to make a key() method, I'd rather just check if the last names are equal and then check first names.
Big thanks to John Meyers, even if it took me a little while to figure what you were on about, I did eventually and I'll copy and paste my solution at the end of this message. As I can't run netbeans on this computer I can't actually test it, but I believe it should work.

I've got this in my Person class. I could've made an external class, but this seems os much easier.
my Arrays.sort() call now considers the start and end index as well as the comperator when necessary.
I agree. Here's the link:
subject: Trying to compare Objects in multiple ways
It's not a secret anymore!