Sorted ListModel

A

Abs

Hi!

I have a ListModel that holds File objects and I'd would like to
sort its elements by name, size, and so on. Right now, I can sort its
elements by only one parameter, but I need my ListModel to be sorted
by more than one. How can I do it ?


Regards
 
B

Bjorn Abelli

...
I have a ListModel that holds File objects and I'd
would like to sort its elements by name, size, and so
on. Right now, I can sort its elements by only one
parameter, but I need my ListModel to be sorted
by more than one. How can I do it ?

You don't mention how you have implemented the ListModel, or how you do the
sorting right now. That makes it possible just to guess what you have, and
what you actually want.

My guess is that you have some backing List using the Collections.sort(List)
method.

Just write a Comparator to sort however you like.

http://java.sun.com/j2se/1.4.2/docs...ml#sort(java.util.List, java.util.Comparator)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html


// Bjorn A
 
A

Abs

Bjorn said:
...




You don't mention how you have implemented the ListModel, or how you do the
sorting right now. That makes it possible just to guess what you have, and
what you actually want.

I've extended DefaultListModel. Right now, I sort the files before
adding them to the model, using a Comparator.
My guess is that you have some backing List using the Collections.sort(List)
method.

Just write a Comparator to sort however you like.

http://java.sun.com/j2se/1.4.2/docs...ml#sort(java.util.List, java.util.Comparator)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html


// Bjorn A

Thanks, I'm beginning to understand the whole thing.


Regards
 
L

Liz

The standard method is to sort by one attribute at a time,
only in reverse order. If you want sort by primary a, secondary b
then sort by b then sort by a.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top