• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort Jtable Column based on two conditions

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have Jtable with two columns.One colimn has following values in it:

987.65 PB
987.65 TB
9.87 GB
987.65 MB
98.76 PB
98.76 TB
9.87 PB
9.87 MB
987.65 GB
9.87 TB
98.76 MB
987.65 kB
98.76 kB
9.87 kB
98.76 GB
0.09 kB
0.00

I have requirement where if i click on column header ,i want column to be sorted in following order:
987.65 PB
98.76 PB
9.87 PB
987.65 TB
98.76 TB
9.87 TB
987.65 GB
98.76 GB
9.87 GB
987.65 MB
98.76 MB
9.87 MB
987.65 kB
98.76 kB
9.87 kB
0.98 kB
0.09 kB
0.00

Thanks,
Nisha
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to utilize javax.swing.table.TableRowSorter and java.util.Comparator. have a look at this tutorial
 
Nisha lakshminaraya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moojid Hamid wrote:You need to utilize javax.swing.table.TableRowSorter and java.util.Comparator. have a look at this tutorial



Ya i am using what ever mentioned in Tutorial. But it will not be generic.I need to hardcode the column number.Can please help me to do so?
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I need to hardcode the column number



Could you please elaborate what do you mean by that? You might want to look at the setSortKeys and setComparator methods of the TableRowSorter.
 
Nisha lakshminaraya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moojid Hamid wrote:

I need to hardcode the column number



Could you please elaborate what do you mean by that? You might want to look at the setSortKeys and setComparator methods of the TableRowSorter.



The sorting i required is for one column ,all other columns should have its default sorting behaviour.
Currently sorting is float values but not on there units so i want sort one column based on float and units as wel.As its for one column only i need to specifically
if (this column then )
use below sorting techinque.

And also i am not very clear about how to sort it based on folat and units values
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As its for one column only i need to specifically
if (this column then )
use below sorting techinque.





And also i am not very clear about how to sort it based on folat and units values



How is the data for that column represented in your TableModel? If it is a String you can split it before doing the comparison in the compareTo method. I would suggest you read the tutorial that I mentioned earlier. Also look up information on how to implement Comparator/Comparable
 
Nisha lakshminaraya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moojid Hamid wrote:

As its for one column only i need to specifically
if (this column then )
use below sorting techinque.





And also i am not very clear about how to sort it based on folat and units values



How is the data for that column represented in your TableModel? If it is a String you can split it before doing the comparison in the compareTo method. I would suggest you read the tutorial that I mentioned earlier. Also look up information on how to implement Comparator/Comparable



I String is displayed in this column. Ya i refered same tutorial .I am using compareTo. I am iteartin and getting column values and then i am doing regular expression to get float value.After this i am not clear what to do
 
Nisha lakshminaraya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nisha lakshminaraya wrote:

Moojid Hamid wrote:

As its for one column only i need to specifically
if (this column then )
use below sorting techinque.





And also i am not very clear about how to sort it based on folat and units values



How is the data for that column represented in your TableModel? If it is a String you can split it before doing the comparison in the compareTo method. I would suggest you read the tutorial that I mentioned earlier. Also look up information on how to implement Comparator/Comparable



I String is displayed in this column. Ya i refered same tutorial .I am using compareTo. I am iteartin and getting column values and then i am doing regular expression to get float value.After this i am not clear what to do



Can any one help me to resolve this issue?
 
reply
    Bookmark Topic Watch Topic
  • New Topic