Computer Science Canada Got some sorting problems. |
Author: | Reality Check [ Mon May 26, 2008 9:12 am ] | ||||
Post subject: | Got some sorting problems. | ||||
I have an array list of friends (from my friend class) and I want to sort them by first name. This is my method for sorting:
I'm calling it like this (f1 is my arraylist):
When I run it, it runs fine and gets past quick sort but it is not sorted for some reason. It comes out the way it went in and I can't figure out where my logic falters. Here is the code in context if you need it. |
Author: | Reality Check [ Mon May 26, 2008 9:48 am ] | ||
Post subject: | Re: Got some sorting problems. | ||
Hmm so I just added more friends and I get an Index out of bounds error when it's more than 1 friend and it runs if its one friend. My method works in a more constrained environment as so:
This works perfectly so I'm not sure why my ArrayList version in my code is giving me problems. |
Author: | Reality Check [ Tue May 27, 2008 7:59 pm ] |
Post subject: | Re: Got some sorting problems. |
Ok I fixed it. It was, as expected, a stupid error that I overlooked. My first while loop should've had "<= 0" and not ">=0" and the second while loop was also the wrong way. Also, my Friend Object declarations should have been under the parent while loop where I wrote "while (k > i)". Because I was making a friend object based on the index k ABOVE this statement, there was the possibility of a negative k and therefore an index out of bounds. I hate these stupid careless errors. But yea, thanks to anyone that was going to answer ![]() |