[ajug-members] Help again
Angus Berry
angus.berry at elken.com
Thu Jul 15 12:27:48 EDT 2004
Mike,
I'm a little late in the game here.. but you'll need to implement the
java.lang.Comparable interface on the class the you want to sort, then
add a compareTo method that returns an int. This is well documented
on-line and in most decent Java books.
A very useful thing about your compareTo method is that you can use 'if'
statements to get fine grained control over your sort based on the
properties of the object being sorted.
Suppose you had a 'Person' object, with firstName, lastName, dateOfBirth
properties. You could set your compareTo method to produce sort that
would sort by:
yearOnly(extracted from the dateOfBirth), lastName, firstName
or
dateofBirth, lastName
etc., etc.
You can control ascending & descending sorts also. I do this in the case
where I need my objects sorted by date with the most recent being first
in my iteration for display.
One final note regarding optimization. If it's at all possible, it's
'generally' going to be more efficient if you can retrieve your objects
in a presorted order from your persistence mechanism to an ordered
collection, as databases are generally, natively good at this type of
operation. In my case an OQL query in my ORM layer can achieve this and
I only need to sort my retrieved collection in certain cases.
Angus
On Wed, 2004-07-14 at 20:23, Barnes, Michael D wrote:
> I need to be able to sort a hashmap when I iterate through the data.
>
> What is the best way to do this?
>
>
> Thanks
> Mike Barnes
>
> *****
>
> The information transmitted is intended only for the person or entity
> to which it is addressed and may contain confidential, proprietary,
> and/or privileged material. Any review, retransmission, dissemination
> or other use of, or taking of any action in reliance upon this
> information by persons or entities other than the intended recipient
> is prohibited. If you received this in error, please contact the
> sender and delete the material from all computers. 117
>
>
>
> ______________________________________________________________________
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
More information about the ajug-members
mailing list