ViewerSorter on View [message #253173] |
Fri, 04 April 2008 15:08 |
Eclipse User |
|
|
|
Originally posted by: andymorton.btinternet.com
Hi.
I have created a view plugin in 3.3 and am able to populate it ok.
I was looking to sort it, and the plug-in wizard created a class called
NameSorter, which extends the ViewerSorter.
However I have no idea how to implement the sort routine.... or even if i
have to!
Any pointers?
Thanks.
Andrew
|
|
|
Re: ViewerSorter on View [message #253176 is a reply to message #253173] |
Fri, 04 April 2008 17:22 |
Wayne Beaton Messages: 554 Registered: December 2017 |
Senior Member |
|
|
You only need a sorter if you want to sort your objects.
Look at the comments on the Comparator class for some hints.
The sorter compares two items and answers 1 if they are in order, 0 if
they are equivalent, or 1 if they are in reverse order (or something
like that; I always have to experiment to get it right).
So... to sort by name (assuming your object has a getName method that
returns a string), override the compare method.
public int compare(Viewer viewer, Object e1, Object e2) {
Person person1 = (Person)e1;
Person person2 = (Person)e2;
return person1.getName().compareTo(person2.getName);
}
You can do funkier things like sort on multiple attributes.
public int compare(Viewer viewer, Object e1, Object e2) {
Person person1 = (Person)e1;
Person person2 = (Person)e2;
int result = person1.getLastName()
.compareTo(person2.getLastName);
if (result == 0) {
return person1.getFirstName()
.compareTo(person2.getFirstName);
} else {
return result;
}
}
This will sort by last name, then first name. Basically, if two objects
have the same last name, the result of the first compare will be 0. In
that case, we sort by the first name. If the result is not 0, we return
it.
HTH,
Wayne
On Fri, 2008-04-04 at 15:08 +0000, Andrew Morton wrote:
> Hi.
> I have created a view plugin in 3.3 and am able to populate it ok.
> I was looking to sort it, and the plug-in wizard created a class called
> NameSorter, which extends the ViewerSorter.
>
> However I have no idea how to implement the sort routine.... or even if i
> have to!
>
> Any pointers?
>
> Thanks.
>
> Andrew
>
|
|
|
Powered by
FUDForum. Page generated in 0.10297 seconds