cancel
Showing results for 
Search instead for 
Did you mean: 

How to use a custom comparator in a TableSorter?

Former Member
0 Kudos

Hi,

I want to use a custom comparator for sorting a table by a specific column.

As you possibly know, the constructor for the TableSorter class looks like this:

TableSorter(IWDTable table, IWDAction sortAction, Map comparators);

As Map is an Interface I chose Hashmap as comparator container. So, I use the put(Key, Value) method to insert my own comparator into the Hashmap in order to deliver this Object to the TableSorter constructor.

But there is an essential problem:

I assume, that the column which is to be associated with my comparator is determined by the key of the Map object from the TableSorter constructor.

Presuming this, <u>what should the map key be/look like?</u>

An Integer counted from the columns? The column header as String? Whatever? Or am I on the wrong way?

PS:

Hours of search did not lead me to some kind of documentation or javadoc of the TableSorter class! This can't be, does someone have a link please?

Thanks a lot for any help.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

(sorry Mrutyun, this did not help.)

Ok, I solved it; let me share it with you:


public class ExampleView 
        public static void wdDoModifyView(IPrivateExampleView wdThis, IPrivateExampleView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)
        { 
                /* 
                 * A custom Comparator class is used for sorting by Severity. 
                 * An Object of it is delivered to an object of a Map class to be delivered to the TableSorter constructor.
                 * The Map class consists of key-value pairs, and the TableSorter must accept the Key of it 
                 * because he uses it to assign the mapped Comparator to the column of the table to be sorted! 
                 * And this is done with the ID of the Element, which shall be sorted with the Comparator, used as Map Key.
                 * All other columns of the assigned tables will be sorted by default Comparators. 
                 */ 
                
                IWDTable table = (IWDTable) view.getElement("[TableName]"); 
                HashMap tableComps = new HashMap(); 
                tableComps.put(view.getElement("[ColumnName]").getId(), new MyComp(wdContext.currentExampleElement())); //The map key value I looked for is the ID of the Element of the Column!
                wdContext.current/*yourContextNode*/Element().setTableSort( 
                        new TableSorter(table, wdThis.wdGetSortTableRowAction(), 
                                tableComps)); //Insert HashMap with the new Comparator
        }

        public void onActionSortTableRow(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ) 
        { 
        //@@begin onActionSortTableRow(ServerEvent) 
                        wdContext.currentConfigurationElement().getTableSort().sort(wdEvent, wdContext.nodeOpenIncident());
        //@@end 
        } 
} 

As you see, the Column which is to be sorted by the custom Comparator "MyComp", is clearly assigned to it. Thus, is will only be used when sorting by this column is done.

The "inActionSort" method is as usual.

My Comparator sorts a column which contains variables of a "SimpleType" with an enumeration. (If you need help with this, feel free to write me.) To achieve this, the Comparator needs to know what this SimpleType consists of. That's why I deliver this Element per Constructor to it.


public class MyComp implements Comparator
{	
	MappedNodeElement element;
	
	
	public SeverityComp(I/*one*/Element element)
	{
		this.element = element;
	}
	
	public SeverityComp(I/*another*/Element element)
	{
		this.element = element;
	}
	
	public int compare(Object severity1, Object severity2)
	{
		//...
	}
}

Because MappedNodeElement is such abstract, it can be used by several different Tables, provided they use the same SimpleType.

Concerning my quest for this resolution, I hope this is helpful.

null

Former Member
0 Kudos

hi

good

go through this links,hope these would help you to solve your problem.

http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2006-05/msg01731.html

http://www.developpez.net/forums/showthread.php?t=13296

thanks

mrutyun^

Message was edited by:

Armin Reichert