Menu

#88 How to use cargo to sort an array in browse

v1.0_(example)
open
nobody
1
2025-04-15
2025-04-13
No

Hi,

I want to sort the array using cargo, but I dont understand it works, and I'm not able to do it.

// ======================================================================
function Calendar(fonction)
// ======================================================================
local oFont := HFont():Add( "Serif",0,-13 ), oIcon
local cTitle := "Calendrier"
local csearch:="", false:=.f., true:=.t.

public oRepas, oCalendar, ndays, cJour, cMois, cAn, bInit:=.t., numcal, mydate, o
public bChange := {||
                   RecupPeriode()
                   return .T.
                  }

oIcon := HIcon():AddFile( cImageDir+"calendrier.png" )

INIT DIALOG oRepas CLIPPER NOEXIT TITLE cTitle AT oMainWindow:nLeft+100,oMainWindow:nTop+130  SIZE 660,230 ;
     FONT oFont ICON oIcon

     // Taille fixe
     hwg_WindowSetResize( oRepas:handle, .F. )

     hwg_SetDlgKey( oRepas, 0, VK_F1, {|| Help("Calendar") } )

     numcal=1

     @ 10, 10 MONTHCALENDAR oCalendar SIZE 300, 160 ON CHANGE bChange TOOLTIP "Cliquez sur une date" INIT date()

     @ 320,9  BROWSE oBrwCal ARRAY SIZE 330,160 STYLE WS_BORDER ;
              ON SIZE ANCHOR_TOPABS + ANCHOR_LEFTABS + ANCHOR_RIGHTABS + ANCHOR_BOTTOMABS ;
              ON POSCHANGE {|o|numcal:=o:nCurrent,Repas()} ;
              ON CLICK {|o|numcal:=o:nCurrent,Menu(aRepas[numcal,1],aRepas[numcal,2],aRepas[numcal,3])} ;
              ON UPDATE {|o|oBrwCal:Refresh(),numcal:=o:ncurrent,Repas()}

     oBrwCal:aArray := aRepas
     oBrwCal:AddColumn( HColumn():New( "     Date ",            {|v,o|o:aArray[o:nCurrent,1]},"C",15,0 ) )
     oBrwCal:AddColumn( HColumn():New( "        Invités      ", {|v,o|o:aArray[o:nCurrent,2]},"C",50,0 ) )

     @ 205,190 BUTTON "Nouveau" OF oRepas ON CLICK {||NewRepas()} ;
               SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
     @ 325,190 BUTTON "Fermer" OF oRepas ID IDCANCEL  ;
               SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")

ACTIVATE DIALOG oRepas

oFont:Release()

return nil

Can you help me ?

Thanks
A+

Related

Support Requests: #88

Discussion

  • Wilfried Brunken

    Hello Alain,
    at this time i had no solution, sorry.

    But try this:
    sort the array in the background with your own function or procedure,
    afterwards call the Refresh() method of the HBROWSE class,
    and the sorted array should now be visible.
    Browsing a database this runs so with success:
    I set with
    SET ORDER TO ...
    an new order (by index) and after refreshing
    the BROWSE window shows the database withbthe new order.
    See logw.prg of CLLOG.

    Have success.

    With regards,
    MNI TNX, 73 es 55 de
    DF7BE, Wilfried

     
  • Aupeix Alain

    Aupeix Alain - 2025-04-14

    Thanks, I have already try it and it works, but only on a column.

    A solution without cargo, could be to have as functions with asort() for each column, but it is then necessary to have as buttons as columns to sort to call these functions.

    I search for another solution ...

    A+

     
    • Wilfried Brunken

      Hello Alain,

      yes, in my CLLOG main program there exists a menu "Sort order" to change
      the order.

      If you found a suitable solution, please inform me by this way,

      at it's best with a sample program, so i can add it to the HWGUI samples

      (also with pre check on Windows and MacOS)

      With regards,
      MNI TNX, 73 es 55 de
      DF7BE, Wilfried

      Am 2025-04-14 21:16, schrieb Aupeix Alain:

      Thanks, I have already try it and it works, but only on a column.

      A solution without cargo, could be to have as functions with asort()
      for each column, but it is then necessary to have as buttons as columns
      to sort to call these functions.

      I search for another solution ...

      A+

      [support-requests:#88] [1] How to use cargo to sort an array in browse

      Status: open
      Group: v1.0_(example)
      Labels: browse array cargo
      Created: Sun Apr 13, 2025 08:32 AM UTC by Aupeix Alain
      Last Updated: Mon Apr 14, 2025 05:43 PM UTC
      Owner: nobody

      Hi,

      I want to sort the array using cargo, but I dont understand it works,
      and I'm not able to do it.

      //

      function Calendar(fonction)
      //
      ======================================================================
      local oFont := HFont():Add( "Serif",0,-13 ), oIcon
      local cTitle := "Calendrier"
      local csearch:="", false:=.f., true:=.t.

      public oRepas, oCalendar, ndays, cJour, cMois, cAn, bInit:=.t., numcal,
      mydate, o
      public bChange := {||
      RecupPeriode()
      return .T.
      }

      oIcon := HIcon():AddFile( cImageDir+"calendrier.png" )

      INIT DIALOG oRepas CLIPPER NOEXIT TITLE cTitle AT
      oMainWindow:nLeft+100,oMainWindow:nTop+130 SIZE 660,230 ;
      FONT oFont ICON oIcon

      // Taille fixe
      hwg_WindowSetResize( oRepas:handle, .F. )

      hwg_SetDlgKey( oRepas, 0, VK_F1, {|| Help("Calendar") } )

      numcal=1

      @ 10, 10 MONTHCALENDAR oCalendar SIZE 300, 160 ON CHANGE bChange
      TOOLTIP "Cliquez sur une date" INIT date()

      @ 320,9 BROWSE oBrwCal ARRAY SIZE 330,160 STYLE WS_BORDER ;
      ON SIZE ANCHOR_TOPABS + ANCHOR_LEFTABS + ANCHOR_RIGHTABS +
      ANCHOR_BOTTOMABS ;
      ON POSCHANGE {|o|numcal:=o:nCurrent,Repas()} ;
      ON CLICK
      {|o|numcal:=o:nCurrent,Menu(aRepas[numcal,1],aRepas[numcal,2],aRepas[numcal,3])}
      ;
      ON UPDATE {|o|oBrwCal:Refresh(),numcal:=o:ncurrent,Repas()}

      oBrwCal:aArray := aRepas
      oBrwCal:AddColumn( HColumn():New( " Date ",
      {|v,o|o:aArray[o:nCurrent,1]},"C",15,0 ) )
      oBrwCal:AddColumn( HColumn():New( " Invités ",
      {|v,o|o:aArray[o:nCurrent,2]},"C",50,0 ) )

      @ 205,190 BUTTON "Nouveau" OF oRepas ON CLICK {||NewRepas()} ;
      SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
      @ 325,190 BUTTON "Fermer" OF oRepas ID IDCANCEL ;
      SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")

      ACTIVATE DIALOG oRepas

      oFont:Release()

      return nil

      Can you help me ?

      Thanks
      A+


      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/hwgui/support-requests/88/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      [1] https://sourceforge.net/p/hwgui/support-requests/88/

       

      Related

      Support Requests: #88

  • Aupeix Alain

    Aupeix Alain - 2025-04-15

    Hi,
    I have tried to use cargo as made in sample in Hwgui Tutor
    Here is the modified code :

    ...
                  oBrwCal:aArray := aRepas
                  oBrwCal:AddColumn( HColumn():New( "     Date ",            {|v,o|o:aArray[o:nCurrent,1]},"C",15,0 ) )
                  oBrwCal:AddColumn( HColumn():New( "        Invités      ", {|v,o|o:aArray[o:nCurrent,2]},"C",50,0 ) )
    
                  oBrwCal:aColumns[1]:bHeadClick := oBrwCal:aColumns[2]:bHeadClick := {|o,n|onHeadClick(o,n)}
                  oBrwCal:aColumns[1]:cargo := oBrwCal:aColumns[2]:cargo := 0
    
       @ 10, 10 MONTHCALENDAR oCalendar SIZE 300, 160 ON CHANGE bChange TOOLTIP "Cliquez sur une date" INIT date()
    
         @ 205,190 BUTTON "Nouveau" OF oRepas ON CLICK {||NewRepas()} ;
                   SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
         @ 325,190 BUTTON "Fermer" OF oRepas ID IDCANCEL  ;
                   SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
    
    ACTIVATE DIALOG oRepas
    
    oFont:Release()
    
    return nil
    
    // ==================================================================
    Function onHeadClick( oBrwCal, nCol )
    // ==================================================================
    local no := Ascan( oBrwCal:aColumns, {|o|o:cargo>0}, 2 )
    
       if no == 0
          oBrwCal:aColumns[nCol]:cargo := 1
       elseif no == nCol
          if oBrwCal:aColumns[no]:cargo == 1
             oBrwCal:aColumns[no]:cargo := 2
          else
             oBrwCal:aColumns[no]:cargo := 1
          endif
       else
          oBrwCal:aColumns[no]:cargo := 0
          oBrwCal:aColumns[nCol]:cargo := 1
       endif
    
       no := nCol
       nCol --
       if oBrwCal:aColumns[no]:cargo == 1
          oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] < y[nCol] } )
       else
          oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] > y[nCol] } )
       endif
       oBrwCal:Refresh()
    
    return Nil
    

    But It fails when running :

    Error BASE/1132  Bound error: array access
    Called from (b)HWG_ERRSYS(19)
    Called from (b)ONHEADCLICK(101)
    Called from ASORT(0)
    Called from ONHEADCLICK(101)
    Called from (b)CALENDAR(64)
    Called from HBROWSE:BUTTONDOWN(1607)
    Called from HBROWSE:ONEVENT(391)
    Called from HWG_ACTIVATEDIALOG(0)
    Called from HDIALOG:ACTIVATE(186)
    Called from CALENDAR(74)
    Called from (b)MAIN(514)
    Called from HOWNBUTTON:MUP(323)
    Called from HOWNBUTTON:ONEVENT(151)
    Called from HWG_ACTIVATEMAINWINDOW(0)
    Called from HMAINWINDOW:ACTIVATE(340)
    Called from MAIN(804)
    

    Where line 101 is :

    oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y|z[nCol]<y[nCol]})
    

    I don't find where is the problem ...

    Thanks for your help

    A+

     
  • Alexander S.Kresin

    It looks like z[nCol] or y[nCol] are out of array range. Check the nCol value and array structure - just add the hwg_writelog() calls to the onHeadClick()

     
  • Aupeix Alain

    Aupeix Alain - 2025-04-15

    Hi,
    I just add a first column :

    // oBrwCal:AddColumn( HColumn():New("",,"C",2,0 ))
    oBrwCal:AddColumn( HColumn():New( "", {|v,o|o:aArray[o:nCurrent,3]}, "C",1,0 ) )
    

    and return nil if the column to sort was the first column
    I tried with the commented line(as in tutor), but it fails

    So here is what is working :

    INIT DIALOG oRepas CLIPPER NOEXIT TITLE cTitle AT oMainWindow:nLeft+100,oMainWindow:nTop+130  SIZE 660,230 ;
         FONT oFont ICON oIcon
    
         // Taille fixe
         hwg_WindowSetResize( oRepas:handle, .F. )
    
         hwg_SetDlgKey( oRepas, 0, VK_F1, {|| Help("Calendar") } )
    
         numcal=1
    
         @ 320,9  BROWSE oBrwCal ARRAY SIZE 330,160 STYLE WS_BORDER ;
                  ON SIZE ANCHOR_TOPABS + ANCHOR_LEFTABS + ANCHOR_RIGHTABS + ANCHOR_BOTTOMABS ;
                  ON POSCHANGE {|o|oBrwCal:Refresh(),numcal:=o:nCurrent,Repas()} ;
                  ON CLICK {|o|numcal:=o:nCurrent,Menu(aRepas[numcal,1],aRepas[numcal,2],aRepas[numcal,3])} ;
                  ON UPDATE {|o|oBrwCal:Refresh(),numcal:=o:ncurrent,Repas()}
    
                  oBrwCal:aArray := aRepas
    
    //            oBrwCal:AddColumn( HColumn():New("",,"C",2,0 ))
                  oBrwCal:AddColumn( HColumn():New( ""                     , {|v,o|o:aArray[o:nCurrent,3]},"C",1,0 ) )
                  oBrwCal:AddColumn( HColumn():New( "     Date "           , {|v,o|o:aArray[o:nCurrent,1]},"C",15,0 ) )
                  oBrwCal:AddColumn( HColumn():New( "        Invités      ", {|v,o|o:aArray[o:nCurrent,2]},"C",50,0 ) )
    
         @ 10, 10 MONTHCALENDAR oCalendar SIZE 300, 160 ON CHANGE bChange TOOLTIP "Cliquez sur une date" INIT date()
    
                  oBrwCal:aColumns[1]:cargo := oBrwCal:aColumns[2]:cargo := oBrwCal:aColumns[3]:cargo := 0
                  oBrwCal:aColumns[1]:bHeadClick := oBrwCal:aColumns[2]:bHeadClick :=oBrwCal:aColumns[3]:bHeadClick := {|o,n|onHeadClick(o,n)}
    
         @ 205,190 BUTTON "Nouveau" OF oRepas ON CLICK {||NewRepas()} ;
                   SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
         @ 325,190 BUTTON "Fermer" OF oRepas ID IDCANCEL  ;
                   SIZE 100, 32 COLOR hwg_ColorC2N("FF0000")
    
    ACTIVATE DIALOG oRepas
    
    oFont:Release()
    
    return nil
    
    // ==================================================================
    Function onHeadClick( oBrwCal, nCol )
    // ==================================================================
    local no := Ascan( oBrwCal:aColumns, {|o|o:cargo>0}, 2 )
    
    if ncol=1
       return nil
    endif
    
    if no == 0
       oBrwCal:aColumns[nCol]:cargo := 1
    elseif no == nCol
       if oBrwCal:aColumns[no]:cargo == 1
          oBrwCal:aColumns[no]:cargo := 2
       else
          oBrwCal:aColumns[no]:cargo := 1
       endif
    else
       oBrwCal:aColumns[no]:cargo := 0
       oBrwCal:aColumns[nCol]:cargo := 1
    endif
    
    no := nCol
    nCol --
    if oBrwCal:aColumns[no]:cargo == 1
       oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] < y[nCol] } )
    else
       oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] > y[nCol] } )
    endif
    oBrwCal:Refresh()
    
    return Nil
    

    I will put a sample that you will be able to improve and add to samples, but it would be nice that Alexander helps use concerning the first column ...

    Thanks
    A+

     

    Last edit: Aupeix Alain 2025-04-15
  • Aupeix Alain

    Aupeix Alain - 2025-04-15

    Hi,
    As Alexander said :

    It looks like z[nCol] or y[nCol] are out of array range. Check the nCol value and array structure - just add the hwg_writelog() calls to the onHeadClick()

    I have modified a bit to debug:

    // ==================================================================
    Function onHeadClick( oBrwCal, nCol )
    // ==================================================================
    local no := Ascan( oBrwCal:aColumns, {|o|o:cargo>0}, 2 )
    
    //if ncol=1
    //   return nil
    //endif
    hwg_WriteLog("-------", "log.txt")
    hwg_WriteLog("ncol (1) = "+ltrim(str(ncol)), "log.txt")
    hwg_WriteLog("no   (1) = "+ltrim(str(no)), "log.txt")
    hwg_WriteLog("-------", "log.txt")
    
    if no == 0
       oBrwCal:aColumns[nCol]:cargo := 1
    elseif no == nCol
       if oBrwCal:aColumns[no]:cargo == 1
          oBrwCal:aColumns[no]:cargo := 2
       else
          oBrwCal:aColumns[no]:cargo := 1
       endif
    else
       oBrwCal:aColumns[no]:cargo := 0
       oBrwCal:aColumns[nCol]:cargo := 1
    endif
    
    hwg_WriteLog("ncol (2) = "+ltrim(str(ncol)), "log.txt")
    hwg_WriteLog("no   (2) = "+ltrim(str(no)), "log.txt")
    hwg_WriteLog("-------", "log.txt")
    
    no := nCol
    nCol --
    if oBrwCal:aColumns[no]:cargo == 1
       oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] < y[nCol] } )
    else
       oBrwCal:aArray := asort( oBrwCal:aArray,,, {|z,y| z[nCol] > y[nCol] } )
    endif
    oBrwCal:Refresh()
    
    return Nil
    

    Here is the array :

    do while .not.eof()
       if ctod(_repas->date) > debut-1 .and. ctod(_repas->date) < fin+1
          aadd(aRepas,{_repas->date,_repas->invites,_repas->menu,iif(_repas->reception,"M","E")})
       endif
       skip
    enddo
    

    And here is the log :

    no = 2
    no = 0
    no = 2
    no = 0
    no = 2
    no = 2
    no = 2
    no = 2
    no = 3
    no = 2
    no = 3
    no = 2
    no = 2
    no = 3
    no = 2
    no = 3
    no = 1
    no = 2
    no = 1
    no = 2
    ncol (1) = 3
    no   (1) = 0
    ncol (2) = 3
    no   (2) = 0
    ncol (1) = 2
    no   (1) = 3
    ncol (2) = 2
    no   (2) = 3
    ncol (1) = 1
    no   (1) = 2
    ncol (2) = 1
    no   (2) = 2
    ncol (1) = 3
    no   (1) = 0
    ncol (2) = 3
    no   (2) = 0
    ncol (1) = 2
    no   (1) = 3
    ncol (2) = 2
    no   (2) = 3
    ncol (1) = 1
    no   (1) = 2
    ncol (2) = 1
    no   (2) = 2
    -------
    ncol (1) = 3
    no   (1) = 0
    -------
    ncol (2) = 3
    no   (2) = 0
    -------
    -------
    ncol (1) = 3
    no   (1) = 3
    -------
    ncol (2) = 3
    no   (2) = 3
    -------
    -------
    ncol (1) = 2
    no   (1) = 3
    -------
    ncol (2) = 2
    no   (2) = 3
    -------
    -------
    ncol (1) = 1
    no   (1) = 2
    -------
    ncol (2) = 1
    no   (2) = 2
    -------
    

    It crashes when clicking on the first column

    And here is the last Error.log :

    Daté du 15/04/25 19:20:35
    ---------------------------------------------
    Error BASE/1132  Bound error: array access
    Called from (b)HWG_ERRSYS(19)
    Called from (b)ONHEADCLICK(113)
    Called from ASORT(0)
    Called from ONHEADCLICK(113)
    Called from (b)CALENDAR(68)
    Called from HBROWSE:BUTTONDOWN(1607)
    Called from HBROWSE:ONEVENT(391)
    Called from HWG_ACTIVATEDIALOG(0)
    Called from HDIALOG:ACTIVATE(186)
    Called from CALENDAR(75)
    Called from (b)MAIN(514)
    Called from HOWNBUTTON:MUP(323)
    Called from HOWNBUTTON:ONEVENT(151)
    Called from HWG_ACTIVATEMAINWINDOW(0)
    Called from HMAINWINDOW:ACTIVATE(340)
    Called from MAIN(804)
    

    Thanks
    A+

     

    Last edit: Aupeix Alain 2025-04-15

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.