wood burning stoves 2.0*
The moose likes Java in General and the fly likes Sort Jtable Column based on two conditions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sort Jtable Column based on two conditions" Watch "Sort Jtable Column based on two conditions" New topic
Author

Sort Jtable Column based on two conditions

Nisha lakshminaraya
Greenhorn

Joined: Feb 23, 2010
Posts: 27
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
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
You need to utilize javax.swing.table.TableRowSorter and java.util.Comparator. have a look at this tutorial
Nisha lakshminaraya
Greenhorn

Joined: Feb 23, 2010
Posts: 27
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

Joined: Mar 07, 2009
Posts: 120
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
Greenhorn

Joined: Feb 23, 2010
Posts: 27
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

Joined: Mar 07, 2009
Posts: 120

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
Greenhorn

Joined: Feb 23, 2010
Posts: 27
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
Greenhorn

Joined: Feb 23, 2010
Posts: 27
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sort Jtable Column based on two conditions
 
Similar Threads
JTable
JTable
JTable
JTable
jtable