Sorting dates in lists and grids

Date:Archived
Product/Release:Visual LANSA
Abstract:Sorting DMY date fields in lists and grids
Submitted By:LANSA Technical Support

Because LANSA dates are stored as numbers, any attempt to use the SortonClick property of a listview or Grid column for a field in DMY format will result the data being sorted incorrectly.

A simple workaround for this issue is to include the same data in YMD format in hidden column. When the user clicks in an attempt to sort the data, the click event for the column will be fired. In the event routine, add code to set the SortPosition and SortDirection properties of the hidden YMD field.

By testing the SortPosition and SortDirection with a simple IF statement, the dates can be forced to go from ascending to descending and vice versa.

See the source below.

Function Options(*DIRECT);
Begin_Com Height(436) Layoutmanager(#ATLM_1) Left(316) Top(115) Width(637);
Define_Com Class(#PRIM_ATLM) Name(#ATLM_1);
Define_Com Class(#PRIM_LTVW) Name(#LTVW_1) Displayposition(1) Fullrowselect(True) Height(409) Left(0) Parent(#COM_OWNER) Tabposition(1) Top(0) Width(629);
Define_Com Class(#PRIM_ATLI) Name(#ATLI_1) Attachment(Center) Manage(#LTVW_1) Parent(#ATLM_1);
Define_Com Class(#PRIM_LVCL) Name(#LVCL_1) Displayposition(1) Parent(#LTVW_1) Sortonclick(True) Source(#EMPNO) Width(20);
Define_Com Class(#PRIM_LVCL) Name(#LVCL_2) Displayposition(2) Parent(#LTVW_1) Sortonclick(True) Source(#SURNAME) Width(20);
Define_Com Class(#PRIM_LVCL) Name(#LVCL_3) Displayposition(3) Parent(#LTVW_1) Sortonclick(True) Source(#GIVENAME) Width(25);
Define_Com Class(#PRIM_LVCL) Name(#LVCL_4) Displayposition(4) Parent(#LTVW_1) Source(#STARTDTE) Width(23);
Define_Com Class(#PRIM_LVCL) Name(#LVCL_5) Parent(#LTVW_1) Source(#STARTDTER) Visible(False) Width(20);
;
Evtroutine Handling(#com_owner.Initialize);
Select Fields(#ltvw_1) From_File(pslmst);
Add_Entry To_List(#ltvw_1);
Endselect;
Endroutine;
;
Evtroutine Handling(#LVCL_4.Click) Options(*NOCLEARMESSAGES *NOCLEARERRORS);
If Cond('(#lvcl_5.sortdirection = Ascending) and (#lvcl_5.sortposition = 1)');
Set Com(#lvcl_5) Sortposition(1) Sortdirection(Descending);
Else;
Set Com(#lvcl_5) Sortposition(1) Sortdirection(Ascending);
Endif;
Endroutine;
End_Com;