This might be a brute force methodology, but it works. :)
public class DisplayTagNumberComparator implements Comparator {
public int compare(Object object1, Object object2) {
int returnValue = 0;
if((object1 instanceof Comparable) && (object2 instanceof
Comparable))
{
if((object1 instanceof String) &&
((String)object1).indexOf("$") != -1 && (object2 instanceof String) &&
((String)object2).indexOf("$") != -1)
try
{
NumberFormat currencyFormatter =
NumberFormat.getCurrencyInstance();
Double d1 = new
Double(currencyFormatter.parse((String)object1).doubleValue());
Double d2 = new
Double(currencyFormatter.parse((String)object2).doubleValue());
returnValue = d1.compareTo(d2);
}
catch(Exception e) { }
else
if((object1 instanceof String) && isNumericStr((String)object1)
&& (object2 instanceof String) && isNumericStr((String)object2))
{
Double d1 = new Double((String)object1);
Double d2 = new Double((String)object2);
returnValue = d1.compareTo(d2);
} else
{
returnValue = ((Comparable)object1).compareTo(object2);
}
} else
if(object1 == null && object2 == null)
returnValue = 0;
else
if(object1 == null && object2 != null)
returnValue = 1;
else
if(object1 != null && object2 == null)
returnValue = -1;
else
returnValue = object1.toString().compareTo(object2.toString());
return returnValue;
}
private boolean isNumericStr(String s)
{
boolean isNumericStr = true;
int dotCount = 0;
if(s != null && s.length() > 0)
{
for(int i = 0; i < s.length(); i++)
{
if(dotCount > 1)
{
isNumericStr = false;
break;
}
if(Character.isDigit(s.charAt(i)))
continue;
if(s.charAt(i) == '.')
{
dotCount++;
continue;
}
isNumericStr = false;
break;
}
} else
{
isNumericStr = false;
}
return isNumericStr;
}
}
Kathryn K. Jones
Email: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
________________________________
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Ferguson, Jason M TSgt 805 CSPTS/SCE
Sent: Monday, December 04, 2006 1:08 PM
To: displaytag-user@lists.sourceforge.net
Subject: Re: [displaytag-user] Sorting in display tag
Can you share it to the mailing list? I have a similar issue that this
may assist with.
Jason
________________________________
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Jones, Kathryn K - IT ADS
Sent: Monday, December 04, 2006 12:04 PM
To: displaytag-user@lists.sourceforge.net
Subject: Re: [displaytag-user] Sorting in display tag
I wrote my own comparator class to solve this problem, and then set it
as the comparator attribute on my displaytag columns. It sorts
currency columns as well as numeric. You can email me for the class
itself if you need it.
Hope that helps!
Kathryn K. Jones
Email: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>
________________________________
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Chaudhary, Harsh
Sent: Monday, December 04, 2006 12:39 PM
To: displaytag-user@lists.sourceforge.net
Subject: Re: [displaytag-user] Sorting in display tag
That's what I currently have. The problem is that it is returning a
String object. So, sorting does not work very well with this.
Harsh.
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Ferguson, Jason M TSgt 805 CSPTS/SCE
Sent: Monday, December 04, 2006 10:40 AM
To: displaytag-user@lists.sourceforge.net
Subject: Re: [displaytag-user] Sorting in display tag
Here's what I did in one of my decorators:
public String getDepreciatedValue() {
Computer currentComputer = (Computer) getCurrentRowObject();
NumberFormat currencyFormat =
NumberFormat.getCurrencyInstance();
return
currencyFormat.format(currentComputer.getDepreciatedValue());
}
________________________________
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Chaudhary, Harsh
Sent: Monday, December 04, 2006 10:33 AM
To: displaytag-user@lists.sourceforge.net
Subject: [displaytag-user] Sorting in display tag
Hi all,
I need to display a table which has columns of strings, dates and
doubles. These need to be sortable. And that's my problem.
For example, the currency column has double values and our DB is such
that these nos could be stuff like 0.25, 9.2, 12355.210E10. But the nos
need to be formatted as 0.25, 9.20, 12355.22.
I implemented a decorator using DecimalFormat object. The problem is
that it returns a string. My question is how to set the no. of decimal
places of a double or Double object to 2 decimal places. I realize this
is not a displaytag question but other people here have got to have come
across this.
If not, how else can I do this?
I will be glad to provide any more specific info if needed.
Thanks,
HC.
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
displaytag-user mailing list
displaytag-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/displaytag-user