Tech Tip: Custom Sorting for ListBox Selections with Calculated Fields
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: April 21, 2025
When you implement custom sorting in listBoxes that include calculated fields, you might notice that changes to the sort state made during the On Header Click event aren’t reliably maintained when the On After Sort event fires. This can lead to two issues:
- Visual Inconsistency: The sort indicator (or icon) only updates after the sort completes.
- Logical Inconsistency: The intended sort direction (e.g., ascending vs. descending) is lost between events.
In this code example vtest is our intermediate variable
var $vp_header : Pointer var $vt_header : Text var $vl_table; $vl_champ; vtest : Integer Case of : (Form event code=On Header Click)  $vp_header:=OBJECT Get pointer(Object current)  RESOLVE POINTER($vp_header; $vt_header; $vl_table; $vl_champ)   If ($vt_header="ovar_firstName_lastName")   Case of   : (($vp_header->=0) | ($vp_header->=1))   $vp_header->:=2   vtest:=2   : ($vp_header->=2)   $vp_header->:=1   vtest:=1   End case   End if : (Form event code=On After Sort)   $vp_header:=OBJECT Get pointer(Object current)   RESOLVE POINTER($vp_header; $vt_header; $vl_table; $vl_champ)   If ($vt_header="ovar_firstName_lastName")    $vp_header->:=vtest   Case of    : (($vp_header->=0) | ($vp_header->=1))    ORDER BY([EMPLOYEES]; [EMPLOYEES]lastName; >)    : ($vp_header->=2)    ORDER BY([EMPLOYEES]; [EMPLOYEES]lastName; <)   End case  End if End case |
Remember, if you frequently encounter such issues, you might also consider alternatives like using a listBox entity selection mode, which may inherently manage calculated fields more gracefully.