[opencms-dev] Override compareTo Method from CmsJspNavElement

Florian Hopf hopf at synyx.de
Tue Aug 26 08:44:57 CEST 2008


Hi,

fritz7 schrieb:
> I have build my own Navigation using CmsJspNavBuilder. Now i would like to sort the list of the CmsJspNavElement's. For this purpose the CmsJspNavElement has already a compareTo Method which could been used by the Comparable Interface.
> 
> Now I would like to use my own compareTo Method whlie override the original.
> 
> Using the @Override Keyword didn't work...
> 
> So what do I have to do?
> 
> 
> This the example I tried to use (the original Method):
> <%!
> 
>    
> @Override public int compareTo(Object obj) {
> 
>         if (obj == this) {
>             return 0;
>         }
>         if (obj instanceof CmsJspNavElement) {
>             float pos = ((CmsJspNavElement)obj).getNavPosition();
>             // please note: can't just subtract and cast to int here because of float precision loss
>             if (m_position == pos) {
>                 return 0;
>             }
>             return (m_position < pos) ? -1 : 1;
>         }
>         return 0;
>     }
> %>
> 
> 

The compareTo method is used only as a method on the object you want to
sort, you can't define it outside of the objects class. What you are
doing is to define the method in the servlet class that is generated
from the jsp.

If you want to implement your own search mechanism using an existing
class (as it's the case with CmsJspNavElement) you can create a class
that implements the interface Comparator. You then sort it using
Collections.sort(yourList, yourComparator).

HTH
Flo





More information about the opencms-dev mailing list