I want to be able to sort these assignments by the date in which they were set.
I can convert the strings into dates and compare them using the .before() method to see which comes first.
My problem is how to swap the assignments around if one is before the other.
I thought about getting the index of the assignments using indexof() and then swapping it with another. . .
not sure if that will work or if theres a better way to do it.
a) iterator.next will move the iterator. So, if you call iterator.next twice inside your loop, it will move 2 steps. Call iterator.next only once if you expect to move it only once
b) This doesn;t look like bubble sort. you are always comparing the date of the first element with the date of the current element. In bubble sort, you compare the date of consecutive elements and swap them.
tom na wrote:Thanks campbell ritchie, i understand why it puts it into an array as it makes the sorting process more efficient. Its going to take me a while to get my head around how to do this. Any hints and tips you could give me for where to start, ive already read the link and searched for a few examples.
Well, Wikipedia has several good pages on various sort algorithms, including bubble sort; however, swapping entries in a LinkedList is somewhat tricky because the class's API doesn't give you access to them (I kind of wish it did). Your idea of using an Iterator is a good one, but I suspect you'll need two of them to perform the swap optimally (ie, without using direct-access methods like add(int, E) and remove(int), which are very slow for LinkedList's).
Another possibility is to look at the source code for Collections.sort(). It doesn't actually use bubble sort, and it's geared for Lists that implement the RandomAccess interface (which LinkedList doesn't), but it might give you some pointers.
Finally, you could write a LinkedList class of your own which does provide access to entries, and then add a
swap(Entry e1, Entry e2)
method. Then a bubble sort should be a doddle.