on 08-06-2007 1:24 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yet to analyse the solutions and work on it. However these seem helpful. Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
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.