/* Header Sort

   Objective: We want to sort data in a tabular or grid datawindow by clicking on the header.
   We will use the fact that PowerBuilder by default names the header for a column
   the same name as the column plus an '_t'. You can change this to something else 
   and you can rename your text fields etc to have an '_t',
   but I will assume that you have not.

   Works on any version PB 5 and above including Pocket PowerBuilder.
   
   Note: If you are doing this because you are not using the PFC... you can easily create
   a function to do this when needed. You choices are: Global function, adding a function 
   to the datawindow, or adding the function to an nvo. For reasons to many to mention here,
   I reccommend that you have an nvo somewhere that you can add this to, something like a 
   n_cst_datawindowmanipulation object. This way you can group functions that relate to manipulating
   the datawindow together ... Ideally you would create something similar to what the pfc has,
   or at think about some pattern. No global functions like gf_headersort. If you are determined to 
   add it to the datawindow control, think of the mess you will have in your descendants. For one
   you will need to have a boolean that controls whether the sort happens or not because all of 
   your datawindows will not be grids or tabular and will not need this functionality.

*/


// Location: the following code in the Clicked event of a datawindow control.

/* First check to ensure the row argument is zero.
   The reason is that pb will pass a zero for the row for anything that was clicked that is not a column.
   Since we are looking for headers, zero is what we want.
*/

if row = 0 then
	// Determine if a column header was clicked.
	if right(dwo.name, 2) = '_t' then
		// Was a column header.
		string ls_newSort
		// get the name of the column to sort by cutting off the '_t'    
		// assume ascending by default.
		ls_newSort = left(dwo.name, len(string(dwo.name)) - 2) + ' A'
		// check if the current sort is this column ascending.
		if ls_newSort = this.object.datawindow.table.sort then
			// if it was, change to descending
			ls_newSort = replace(ls_newSort, len(ls_newSort), 1, 'D')
		end if
		// set the sort
		this.object.datawindow.table.sort = ls_newSort
		// sort
		this.sort()
	end if
end if

// the end

