Java Programming - Sorting LinkedList

Hi!

I'm trying to sort a LinkedList.

That LinkedList contains some objects called packet, each packet have an attribute called hour.

I'd like to sort the LinkedList by the hour component of its objects(packets).

I've no idea how to do it using any API, I've tried with Collections.sort(...) but it doesn't compiles....

Could anybody post anything such an example?

Thank you very much!

[433 byte] By [tashnuitaria] at [2007-11-26 23:32:07]
# 1

Either Packets implements java.lang.Comparable and you do this:

Collections.sort(yourList);

Or you define a java.util.Comparator that orders Packets by hour and you do this:

Collections.sort(yourList, yourComparator);

There are examples of sorting in the algorithm section of the collections tutorial:

http://java.sun.com/docs/books/tutorial/collections/index.html

Message was edited by:

DrLaszloJamf

DrLaszloJamfa at 2007-7-10 14:44:28 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you very much! It has been really useful :D
tashnuitaria at 2007-7-10 14:44:28 > top of Java-index,Java Essentials,Java Programming...
# 3
You're welcome.
DrLaszloJamfa at 2007-7-10 14:44:28 > top of Java-index,Java Essentials,Java Programming...