cancel
Showing results for 
Search instead for 
Did you mean: 

Usage of the Node-Function "sortElements"

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi Valery,

GREAT. That solves my problem immediately.

FF2S is nice ;-).

Thanks,

Claus

Former Member
0 Kudos

Claus, btw, the style of the license encourage assigning reward points

VS

Former Member
0 Kudos

Ups...i thought i have still assigned....RAPPELZING again...

CB

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

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

Former Member
0 Kudos

Valery! Thank you very much for this algorithm. Saved me a lot of work!

Keep up the good work!

Regards,

Hans Petter Bjørn

Former Member
0 Kudos

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...

Former Member
0 Kudos

Hanoz,

Make sure that all 3 classes has same package

VS

Former Member
0 Kudos

hi,

Hey i did all that, it was kind of obvious.

the problem was in the naming of the java files that i had created.

i renamed them to the name of the java classes in them.

Thanx for the prompt response anyways

Regards,

hanoz

ashish_shah
Contributor
0 Kudos

Hi Valery,

Can you take a look at this thread.

[;

I am using the classes suggested by you under FF2S licence

I am encountering the problem in lead selction.

Thanks in advance.

Ashish Shah

Answers (0)