cancel
Showing results for 
Search instead for 
Did you mean: 

Node Sorting

Former Member
0 Kudos
77

Hi,

I have a Value Node which is populated dynamically by 2 model node data. Now I want to sort that value node before display.

I have implemented Table Sorter, but in this sorting happen after event on Table Column Header.

Thank you.

Regards,

Amit

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Amit,

There is one method called <i>sortElements().</i>

You can use it like:

wdContext.node<node name>().sortElements(Comparator arg0);

As you have noticed that this method takes a Comparator as the argument. You can either write your own Comparator or use the Comparator in TableSorter.

Regards,

Satyajit.

Former Member
0 Kudos

Thanks to all for the quick reply.

Satyajit,

Can you please brief how to use Comparator in Tablerorter.java

Thank you.

Amit

former_member182372
Active Contributor
0 Kudos

Amit, why don`t you like Valery`s solution? It is general, flexible, simple and read-to-use.

Former Member
0 Kudos

Amit,

What I meant is, in the

//@@begin others
//@@end

part of your code, just use this code:


private static final Comparator DEFAULT = new Comparator() {
		
	public int compare(Object o1, Object o2) {
		if (o1 == null && o2 == null)
			return 0;
		if (o1 == null)
			return -1;
		if (o2 == null)
			return +1;
		if (o1 instanceof Boolean && o2 instanceof Boolean)
			return o1.toString().compareTo(o2.toString()); // false < true
		if (o1 instanceof String && o2 instanceof String){
			//Use a Collator for sorting according to the given Locale
			Collator collate = Collator.getInstance(WDResourceHandler.getCurrentSessionLocale());
			return collate.compare(o1, o2);				
		}
		return ((Comparable) o1).compareTo((Comparable) o2);
	}
};

Then while calling sortElements, call it as


wdContext.node<node name>().sortElements(DEFAULT);

Regards,

Satyajit.

Former Member
0 Kudos

Hi Satyajit,

I am getting runtime error :

wdContext.node<node name>().sortElements(DEFAULT);

Should I change DEFAULT with something... Sorry if I am talking nonsense.

Thank You.

Amit

Former Member
0 Kudos

Hi,

In that case try this:


private final class MyComparator implements Comparator{
		
	public int compare(Object o1, Object o2) {
		if (o1 == null && o2 == null)
			return 0;
		if (o1 == null)
			return -1;
		if (o2 == null)
			return +1;
		if (o1 instanceof Boolean && o2 instanceof Boolean)
			return o1.toString().compareTo(o2.toString()); // false < true
		if (o1 instanceof String && o2 instanceof String){
			//Use a Collator for sorting according to the given Locale
			Collator collate = Collator.getInstance(WDResourceHandler.getCurrentSessionLocale());
			return collate.compare(o1, o2);				
		}
		return ((Comparable) o1).compareTo((Comparable) o2);
	}
}

Then while calling sortElements, call it as


wdContext.node<node name>().sortElements(new MyComparator());

Regards,

Satyajit.

Message was edited by: Satyajit Chakraborty

Message was edited by: Satyajit Chakraborty

Former Member
0 Kudos

Hi Satyajit,

Still I am getting runtime error

java.lang.ClassCastException

Regards,

Amit

Former Member
0 Kudos

Hmmm...

What if you remove this line?

return ((Comparable) o1).compareTo((Comparable) o2);

Regards,

Satyajit.

Former Member
0 Kudos

Its givig compilation error.

Former Member
0 Kudos

Just remove that line and replace with:

return 1;

Regards,

Satyajit.

Former Member
0 Kudos

You must pass a Comparator implementation that compares <b>IWDNodeElement </b>instances.

Armin

Former Member
0 Kudos

Its not giving any error .. but the sorting is not as desired. I want to sort on the basis of first column.

Regards,

Amit

Former Member
0 Kudos

private class CustomComparator implements Comparator
 {
	String attribute;
	int sortOrder;

	CustomComparator(String attrib,int sort)
	{
		this.attribute = attrib;
		if(sort == 0) sort = 1;
		this.sortOrder = sort;
	}

	public int compare(Object o1, Object o2)
	{
		IWDNodeElement element1 = (IWDNodeElement) o1;
		IWDNodeElement element2 = (IWDNodeElement) o2;

		return element1.getAttributeAsText(attribute).compareToIgnoreCase(
				element2.getAttributeAsText(attribute))
				* sortOrder;
	}

	public boolean equals(Object o1, Object o2) {
		IWDNodeElement element1 = (IWDNodeElement) o1;
		IWDNodeElement element2 = (IWDNodeElement) o2;
		if (element1
			.getAttributeAsText(attribute)
			.equalsIgnoreCase(element2.getAttributeAsText(attribute)))
			return true;
		else
			return false;
	}
 }

Call this as


  CustomComparator comp = new CustomComparator(sortAttribute,1);
  wdContext.node<node Name>().sortElements(comp);
 

Here sortAttribute is the context attribute that is bound to your first column. So, if your context attribute that is bound to your first column is called "EmployeeName", then call should be like


   CustomComparator comp = new CustomComparator("EmployeeName,1);
   wdContext.node<node Name>().sortElements(comp);
 

Regards,

Satyajit.

Message was edited by: Satyajit Chakraborty

Answers (2)

Answers (2)

former_member182372
Active Contributor
0 Kudos

Hi Amit,

Check guru`s solution

Best regards, Maksim Rashchynski.

sridhar_k2
Active Contributor
0 Kudos

Hi Amit,

If you want Sorting before display, you have to go for the manual process.

Get that node values(Objects) in your program, and sort it and assign it to the table. But it requires coding.

Regards,

Sridhar