Collections.sort - do i *have* to implement List

T

timasmith

I have custom collection of my objects

MyModelList list;

which I wish to sort based upon the contents - which implement
Comparable.

Collections.sort(list);

is a beautiful thing - BUT to implement the list interface is asking a
bit much... there are 20 methods or more...

Extending Vector is not an option for me since I already extend the
AbstractTableModel so I can easily view my list in a JTable.

What else can I do to elegantly sort my custom list?

thanks

Tim
 
B

bugbear

I have custom collection of my objects

MyModelList list;

which I wish to sort based upon the contents - which implement
Comparable.

Collections.sort(list);

is a beautiful thing - BUT to implement the list interface is asking a
bit much... there are 20 methods or more...

Extending Vector is not an option for me since I already extend the
AbstractTableModel so I can easily view my list in a JTable.

What else can I do to elegantly sort my custom list?

If your class *contains* an (instance of) list, and you sort that,
I think you're where you want to be.

BugBear
 
C

Chris Uppal

I have custom collection of my objects

MyModelList list;

which I wish to sort based upon the contents - which implement
Comparable.

Collections.sort(list);

is a beautiful thing - BUT to implement the list interface is asking a
bit much... there are 20 methods or more...

You could create an adapter object which implements List by forwarding to your
MyModelList. The best way to do that is probably to start with
java.util.AbstractList, and then override the get(int), size(), and set(int,
Object) methods. If your MyModelList doesn't have constant-time access, then
it may be better to start with java.util.AbstractSequentialList instead.

If you are just wanting to sort the contents of a JTable, then google for
java jtable sort column
to find alternative, and maybe preferable, approaches.

-- chris
 
T

Thomas Weidenfeller

I have custom collection of my objects

MyModelList list;

which I wish to sort based upon the contents - which implement
Comparable.

Collections.sort(list);

is a beautiful thing - BUT to implement the list interface is asking a
bit much... there are 20 methods or more...

Extending Vector is not an option for me since I already extend the
AbstractTableModel so I can easily view my list in a JTable.

What else can I do to elegantly sort my custom list?

You can at least:

- Subclass AbstractList instead of subclassing AbstractTableModel, and
implement TableModel

- You can provide a method which handles the sorting internally,
assuming you do have a List or an Object[] inside your class:

void sort() {
[Arrays|Collections].sort(theListsInternalData);
}

- Use Arrays.sort() if you have your list elements in some form of Object[].

- Implement some sorting algorithm by your own.

- If you internally have a List or Object[], implement a method (not
recommended):

[List|Object[]] getList() {
return theListsInternalDataStructure;
}

- Implement List

/Thomas
 
T

Thomas Hawtin

Thomas said:
Collections.sort(list);

is a beautiful thing - BUT to implement the list interface is asking a
bit much... there are 20 methods or more...

Extending Vector is not an option for me since I already extend the
AbstractTableModel so I can easily view my list in a JTable.

What else can I do to elegantly sort my custom list?

You can at least:

- Subclass AbstractList instead of subclassing AbstractTableModel, and
implement TableModel

- [...]

Create an array of Integer representing current indexes. Fill the array
with 0, 1, 2, etc. Create a Comparator<Integer>, containing a
reference to your model, that sorts indexes based on data from your
model. Use Arrays.<Integer>sort(Integer,Comparator<Integer>). Rearrange
your model data from array of indexes.

Okay, perhaps not an elegant solution. More of a hack. The point is,
data used to sort an array/List need not be directly referenced by the
collection. More generally keeping a key to data can be more effective
than keeping a na\"ive reference (say, if you have a terabyte database).

Tom Hawtin
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top