on 02-07-2005 10:45 AM
Hi,
i would like to sort a context node by 3 attributs of that node.
I found the function "sortElements" of a Context node.
But how do i have to create the corresponding Comparator-Object for that function ? Or is there another way to sort the context nodes ?
Regards,
Claus
Well, besides sorting you can yourself add elements in sorted order
Comparator creation is very straigtforward:
public class MyNodeElementsComparator implements java.util.Comparator
{
public int compare(final Object o1, final Object o2)
{
final IPrivate<MyControllerName>.I<MyNodeName>Element el1 =
(IPrivate<MyControllerName>.I<MyNodeName>Element)o1;
final IPrivate<MyControllerName>.I<MyNodeName>Element el2 =
(IPrivate<MyControllerName>.I<MyNodeName>Element)o2;
return el1.getMyStrAttr().compareTo(el2.getMyStrAttr());
/*
return el1.getMyIntAttr() - el2.getMyIntAttr();
*/
}
}
Or we can make it more generic and utilize Comparable interface:
package net.sdn.utils.context;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;
/**
* @author Valery_Silaev
*/
class OrderingAttribute
{
final String name;
final boolean ascending;
OrderingAttribute(final String attribute, final boolean ascending)
{
if (null == attribute)
throw new IllegalArgumentException("Attribute name may not be null");
this.name = attribute;
this.ascending = ascending;
}
public int hashCode() { return name.hashCode(); }
public boolean equals(final Object o)
{
if ( o == null || o.getClass() != this.getClass() )
return false;
final OrderingAttribute other = (OrderingAttribute)o;
return this.name.equals( other.name );
}
}
public class OrderingExpression
{
final private Set attrs = new LinkedHashSet();
public OrderingExpression asc(final String attribute)
{
attrs.add( new OrderingAttribute(attribute, true) );
return this;
}
public OrderingExpression desc(final String attribute)
{
attrs.add( new OrderingAttribute(attribute, false) );
return this;
}
Iterator iterator() { return attrs.iterator(); }
}
public class GenericNodeElementComparator implements Comparator
{
final private OrderingExpression expression;
public static GenericNodeElementComparator asc(final String attribute)
{
return new GenericNodeElementComparator( new OrderingExpression().asc(attribute));
}
public static GenericNodeElementComparator desc(final String attribute)
{
return new GenericNodeElementComparator( new OrderingExpression().desc(attribute));
}
public GenericNodeElementComparator(final OrderingExpression expression)
{
this.expression = expression;
}
public int compare(final Object o1, final Object o2)
{
final IWDNodeElement el1 = (IWDNodeElement)o1;
final IWDNodeElement el2 = (IWDNodeElement)o2;
for (final Iterator i = expression.iterator(); i.hasNext(); )
{
final OrderingAttribute orderBy = (OrderingAttribute)i.next();
final Comparable val1 = (Comparable)el1.getAttributeValue( orderBy.name );
final Comparable val2 = (Comparable)el2.getAttributeValue( orderBy.name );
final int delta = val1.compareTo( val2 );
if ( delta != 0)
return orderBy.ascending ? delta : -delta;
}
return 0;
}
}
Usage:
node.sortElemenst( GenericNodeElementComparator.asc("Name") );
node.sortElemenst( GenericNodeElementComparator.desc("CreatedAt") );
node.sortElemenst
(
new GenericNodeElementComparator
(
new OrderingExpression().desc("Salary").asc("Name")
)
);
Code is released under FF2S license (feel free to steal
VS
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Valery,
consider utilizing the java.text.Collator class for sorting context node elements.
Locale locale = WDClientUser.getCurrentUser().getLocale()
...
public class TextComparator implements Comparator {
private Collator collator;
/**
* Constructor for TextComparator. Need local-information for correct sorting.
*/
public TextComparator(Locale locale) {
super();
this.collator = Collator.getInstance(locale);
}
/**
* @see java.util.Comparator#compare(Object, Object)
*/
public int compare(Object o1, Object o2) {
IPrivate<MyControllerName>.I<MyNodeName>Element e1 = (IPrivate<MyControllerName>.I<MyNodeName>Element) o1;
IPrivate<MyControllerName>.I<MyNodeName>Element e2 = (IPrivate<MyControllerName>.I<MyNodeName>Element) o2;
return collator.compare(e1.getText(), e2.getText());
}
}
Otherwise the comparison won't work for most languages.
Regards, Bertram
Hi Valery,
I tried out your generic code:
I made three java files for the three java classes:OrderingAttribute
, OrderingExpression, GenericNodeElementComparator
the first two java files are error free.
However the third java file: GenericNodeElementComparator throws the following error:OrderingExpression cannot be resolved(or is not a valid type) for the field GenericNodeElementComparator.expression
at the expression: final private OrderingExpression expression;
some help please...
User | Count |
---|---|
98 | |
11 | |
11 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.