cancel
Showing results for 
Search instead for 
Did you mean: 

How to sort the elements of Drop down list?

0 Kudos

Hello,

I create DropDownByKey element in Webdynpro. I want to sort elements in the drop down list.

In wdDoInit() method i have following code:

...........

IWDNodeInfo componentsNodeInfo = omponentsNode.getNodeInfo();

IWDAttributeInfo originalAI = componentsNodeInfo.getAttribute("original");

ISimpleTypeModifiable originalComp = originalAI.getModifiableSimpleType();

originalCompSet = originalComp.getSVServices().getModifiableSimpleValueSet();

for (int i = 0; i < compotibleComps.size(); i++) {

ISoftwareComponent component =(ISoftwareComponent) compotibleComps.get(i);

originalCompSet.put("Key_" + i, component.getDisplayname());

components.put("Key_" + i, component);

}

//sort components

originalCompSet.sort(this.getComparator());

.......

private Comparator getComparator() {

Comparator result = new Comparator() {

public int compare(Object source, Object target) {

}

};

return result;

}

An instance of which class is the "source"? How can i implement compare method?

regards, Krasimira

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi krasmira

I think it should be

private Comparator getComparator() {
Comparator result = new Comparator() {
public int compare(Object source, Object target) {
 String firstDisplayName =  (String)source; 
 String secondDisplayName =  (String)target;
 return firstDisplayName.compareTo(secondDisplayName);
}
};
}

Regards

Pran

0 Kudos

Hi Pran,

The ClassCastException is thrown when i used your code.

When i was debugging the source was instance of com.sap.typeservices.SimpleValueSet#Entry. But i don't how can i get Entry.

Regards, Krasimira

Former Member
0 Kudos

Oops sorry for that I should have checked

An easier way out would be to pre sort your compotibleComps list using Collections.sort(compotibleComps,getComparator()); In which case

private Comparator getComparator() {
Comparator result = new Comparator() {
public int compare(Object source, Object target) {
 String firstDisplayName =  ((ISoftwareComponent)source).getDisplayName(); 
 String secondDisplayName =  ((ISoftwareComponent)target).getDisplayName();
 return firstDisplayName.compareTo(secondDisplayName);
}
};
}

Regards

Pran

0 Kudos

Hi Pran,

Thanks a lot.

I will do in this way.

But i want to know how can i sort drop down list

Is there possibility to do this?

Former Member
0 Kudos

import java.util.Map;
...
private Comparator getComparator(final boolean byKey) 
{
  return new Comparator() 
  {
    public int compare(final Object o1, final Object o2) 
    {
      final Map.Entry e1 = (Map.Entry)o1; 
      final Map.Entry e2 = (Map.Entry)o2;

      if (byKey) /* Sort by key */
      {  
        final Comparable k1 = (Comparable)e1.getKey(); 
        final Comparable k2 = (Comparable)e2.getKey();
        return k1.compareTo(k2);
      }
      else /* Sort by value (display name) */
      {  
        final Comparable v1 = (Comparable)e1.getValue(); 
        final Comparable v2 = (Comparable)e2.getValue();
        return v1.compareTo(v2); 
      }
    }
  };
}

NULL handling ommited from example

Regards,

VS

0 Kudos

Hello Valery,

Thanks for your help.

Regards,

Krasimira

Former Member
0 Kudos

@your srvc

Also you can use yellow star icon on my message to say "thanks"

VS