How to sort a DefaultListModel
In this reply I assume that "sort it like (parameter2,parameter1,parameter3)"
means to reorder the elements of the model in some way.
No matter how you want to define the ordering, you only need to write a method
to compare two elements and decide which should come before the other.
First you make the Class have the Comparable interface
and define the comparison method:
Code:
class Class implements Comparable<Class> {
...
// the compareTo method is defined by the Comparable interface
int compareTo(Class o) {
if ( [I][COLOR="DarkOrchid"]this comes before o[/COLOR][/I] ) return -1;
else if ( [COLOR="DarkOrchid"]this comes after o[/COLOR] ) return +1;
return 0; // it is best if this case only happens when this == o
}
...
} // end class Class
Then actual sorting can be done with a method like this
Code:
static void sort(DefaultListModel dlm) {
Class [] dlma = dlm.toArray(); // make an array of the elements in the model
Arrays.sort(dlma); // sort the array (this step uses the compareTo method)
dlm.clear(); // empty the model
for (Class x : dlma)
dlm.add(x); // insert all the elements into the model in sorted order
}