2

I have a JTable with follow values:

Name        age        money

  A          13         SGD23

  B          41         SGD133

If I click the Header(Name), it will sort as A,B, click again, will be B,A

But for the money, if I click it, I want to sort the values like SGD 23,SGD 133 . However, the Swing TableRowSort sorts the values like SGD 133,SGD23 . It is because the column value class is String. So can anybody help me to do this? Please note the money has CurrencyCode SGD.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Sstx
  • 604
  • 1
  • 8
  • 21
  • visit this [link](http://stackoverflow.com/questions/2075396/correctly-getting-data-from-a-sorted-jtable) may be it will usefull for you – Engineer Oct 22 '13 at 05:23
  • 1
    there are three ways, everything is about design only 1. put currency to separate column, then is possible to combine any currencies, 2. if is there only one currency SGD, then to use Locale, Formatter and Renderer, 3. create custom Comparator – mKorbel Oct 22 '13 at 07:21

1 Answers1

2

You'll need to implement a Comparator which knows how to sort the values in your column, then apply it using DefaultRowSorter.setComparator(...)

Another approach would be to only store the numeric portion of your currency and apply the CurrencyCode as part of the CellRenderer. That way you could simply sort on numeric values rather than trying to tweak the String Comparator to suit your needs.

Jason Braucht
  • 2,358
  • 19
  • 31
  • I have tried tablePanel.getJTable().setAutoCreateRowSorter(true); ((TableRowSorter>)tablePanel.getJTable().getRowSorter()).setComparator(AIR_COL_MU, new Comparator() { @Override public int compare(Object o1, Object o2) { if (o1 == o2) { } return 0; } }); – Sstx Oct 22 '13 at 05:19
  • but when I debug it, can't break at compare method – Sstx Oct 22 '13 at 05:20
  • @SStx Could you update your question with your code or a even better, a [SSCCE](http://sscce.org/)? It's difficult to give you specific advice without seeing the code. – Jason Braucht Oct 22 '13 at 13:37