on 04-14-2005 9:47 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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
User | Count |
---|---|
85 | |
10 | |
10 | |
10 | |
7 | |
6 | |
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.