cancel
Showing results for 
Search instead for 
Did you mean: 

Problem in sorting with date

Former Member
0 Kudos

Hi,

I have a requirement of sorting a column which displays date or a comment. Because of the presence of comment in some of the rows, the data type is not maintained as Date but as String.

That is cause of problem as the dates are getting sorted as per the String rules.

Could anyone suggest an approach of sorting strings with date??

Many Thanks,

Anagha

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Write your own Comparator that knows how to handle the possible formats in that column.

The semantics might be

compare(String x, String y) =
  x.compareTo(y),                                isComment(x) && isComment(y)
  -1,                                                    isComment(x) && isDate(y)
  1,                                                     isDate(x) && isComment(y)
  toDate(x).compareTo( toDate(y) )        isDate(x) && isDate(y)

Implement predicates isComment() and isDate() as given by the possible formats of your data.

Armin

Answers (2)

Answers (2)

Former Member
0 Kudos

Yet to analyse the solutions and work on it. However these seem helpful. Thanks.

Former Member
0 Kudos

Hello Anagha!

Try to this code.


class MyComparator implements Comparator {
	public int compare(Object o1, Object o2) {
	IWDNodeElement element1 = (IWDNodeElement) o1;
	IWDNodeElement element2 = (IWDNodeElement) o2;
	Date date1 = new Date(element1.getAttributeAsText("Your name of Attribute"));
	Date date2 = new Date(element2.getAttributeAsText("Your name of Attribute"));
	return date1.compareTo(date2);
	}

And just use it for your node, like this


    I"YourNode"Node node = wdContext.node"YourNode"();
    MyComparator myComparator = new MyComparator();
    node.sortElements(myComparator);

I hope it helps!

Message was edited by:

Vitali Bashko