This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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 ]
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?
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.
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.
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.