cancel
Showing results for 
Search instead for 
Did you mean: 

sorting dates by TableSorter

Former Member
0 Kudos

Hi,everybody,

My problem is sorting table by one column named Last_Visit that

must be sorted by date in descending order.I recieve the dates from

data base like string(String str = runQuery(...)),becouse I have empty

values too(person haven'tvisits,never was).And after that I do the next:

DBformat ("yyyy-MM-dd-mm-ss") is like in data base

normalShowDateFormat (dd-MM-yyyy) is what I need to show in table

Date dt = DBformat.parse(str);

row.setLastVisit(normalShowDateFormat.format(dt));

LastVisit is defined of type string in the context.

The problem is that the column is sorted by TableSorter like column

consists of strings,but I need to sort it by dates(comparing first year,

second month,third day).I also do the trik by comparing dates in format

yyyy-MM-dd,but that what I see also on the screen and I need to see

dd-MM-yyyy.

Any ideas?

Regards,

Michael

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

hi mic..,

U have retrived the fields as a string so its difficult.. try my suggestion

1-> retrive the field and store it as a seprate column for date , month and year.. using the query..

for example..

if the field name is "lstdtvist"..

then the the date will be of 3 character..

retrive it by giving lstdtvist2(9)

this will give u the first date.. then repeate it for month and year..

u can then write a query using order by ..

try and reply back i can do for u..

Former Member
0 Kudos

Hai Michael,

You can dispaly Dateformat by using simpletype. create a simpletype of base type date in representation tab in format you can gve like dd-MM-yyyy.

if youare using nw 2004s in the properties of a cloumn you can change sort state to up.

Regards,

Naga

venkatakalyan_karanam
Active Contributor
0 Kudos

Hi beilin

Th column consists of dates you can sort just by using TableSorter.java with small modifications in the Comparator class,This class object ref you have to pass to your method sortElements (Comparator c) of the IWDNode API.If you have the code of TableSorter.java then see the comparator and just write Comparision of Dates and the node dataSource of the table should bind with dataSource property of Table.There is a one more attr that you have to bind Column sortDirection you should bind,So that you can know which direction the user wish to sort.See the forums which will give you TableSorter.java .If you dont have that give me your mail id,I will send you the entire class.

thanks and regards

kalyan

former_member182372
Active Contributor
0 Kudos

Hi Michael,

Third parameter in TableSorter`s constructor is array of Comparator`s (Comparator[] comparators). So, implement comparator which takes "dd-MM-yyyy", parse it as Date and compares Dates instead of Strings. While instantiating new TableSorter pass new instance of Comparator to array with the same index as your column.


  public static final SimpleDateFormat sdf = new SimpleDateFormat ("dd-MM-yyyy"); 
  public final class FormattedDateComparator implements Comparator {
  		public int compare(Object o1, Object o2) {
			String val1 = (String)o1;
			String val2 = (String)o2;
			
			Date date1 = null;
			Date date2 = null;
			 
			try{date1 = sdf.parse(val1);}catch(Exception e){}
			try{date2 = sdf.parse(val2);}catch(Exception e){}
			
			if(null!=date1) {
				if(null!=date2) {
					return date1.compareTo(date2);
				} else {
					return 1;
				}
			} else {
				if(null!=date2) {
					return -1;
				} else {
					return 0;
				}
			}
	  }
  }

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

Hi,Maksim

Thanks fo your reply,it was very usefull.

I have one more question.In my requiment I was need to sort my

table once by specific column,so I simply replaced the original

comparator in the TableSorter.java by your comparator.But if I need

to sort my table still by different columns selected by user,how can

I leave both comparators and TableSorter will know which of them to

activate in particular case?

Regards,

Michael

former_member182372
Active Contributor
0 Kudos

Hi Michael,

I don`t really understand your question.

>>In my requiment I was need to sort my table once by specific column

You can do it directly using IWDNode.sortElements() method and pass instance of your specific Comparator

>>But if I need to sort my table still by different columns selected by user,how >>can I leave both comparators and TableSorter will know which of them to

>>activate in particular case

As I said before, TableSorter has array of Comparators as construcor parameter. Let say in your table you need specific custom behavior while sortig for column number 3 (formated date as string). Then you should do something like this:


new TableSorter(..., ..., new Comparator[]{null, null, new SpecificComparator(), null.....});

Hope it makes clearer.

Best regards, Maksim Rashchynski.

Former Member
0 Kudos

hi michael,

can u post me with screen, so that i can try to solve as it would be help ful to solve the problem