FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour xBrowse autosort 10.6 with object
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
xBrowse autosort 10.6 with object
Posted: Thu Mar 22, 2012 01:05 PM

Hi, I want to use the xBrowse autosort (for array) but when I use an object as an array the sort just don't work...

I put a debug in ACompare() and xVal and yVal is always numeric and 1, any tips?

Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: xBrowse autosort 10.6 with object
Posted: Thu Mar 22, 2012 05:51 PM
Please try
Code (fw): Select all Collapse
#include "fivewin.ch"
#include "xbrowse.ch"

function main()

   local aObj, oDlg, oBrw, oFont

   aObj  := { ;
      TTemp():New( 1, 'One',   Date()     ), ;
      TTemp():New( 5, 'Five',  Date() + 2 ), ;
      TTemp():New( 3, 'Three', Date() - 3 )  }

   SET DATE ITALIAN
   SET CENTURY ON

   DEFINE FONT oFont NAME "TAHOMA" SIZE 0,-14
   DEFINE DIALOG oDlg SIZE 400,200 PIXEL FONT oFont

   @ 10,10 XBROWSE oBrw SIZE -10,-10 PIXEL OF oDlg ;
      FIELDS oBrw:aRow:nVal, oBrw:aRow:cVal, oBrw:aRow:dVal ;
      HEADERS "Number", "CharVal", "Date" ;
      SORT { |o| MyColSort( o ) }, { |o| MyColSort( o ) }, { |o| MyColSort( o ) } ;
      COLSIZES 50,100,100 ;
      ARRAY aObj CELL LINES NOBORDER AUTOSORT

   WITH OBJECT oBrw
      :nStretchCol   := STRETCHCOL_WIDEST
      :CreateFromCode()
   END

   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont

return nil


static function MyColSort( oCol )

   local cOrder   := If( oCol:cOrder == 'A', 'D', 'A' )
   local oBrw     := oCol:oBrw
   local cData    := { "nVal", "cVal", "dVal" }[ oCol:nCreationOrder ]

   if cOrder == 'D'
      ASort( oBrw:aArrayData,,,{ |x,y| OSend( x, cData ) > OSend( y, cData ) } )
   else
      ASort( oBrw:aArrayData,,,{ |x,y| OSend( x, cData ) < OSend( y, cData ) } )
   endif

return cOrder


CLASS TTemp

   DATA nVal, cVal, dVal

   METHOD New( a, b, c ) CONSTRUCTOR

ENDCLASS

METHOD New( a, b, c ) CLASS TTemp

   ::nVal   := a
   ::cVal   := b
   ::dVal   := c

return Self
Regards



G. N. Rao.

Hyderabad, India
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: xBrowse autosort 10.6 with object
Posted: Thu Mar 22, 2012 09:20 PM
Hi my structure is this:

Code (fw): Select all Collapse
   oRecto := TRecebimento():New()
   
   //-- xBrowse ----------------------------------------------------------//
   
   oBrx := TXBrowse():New( oDlg )
   oBrx:CreateFromResource( 301 )
    
   oBrx:lHScroll := .T. //-- 4/5/2011 - Aloizio.

   //-- Obs - aTitulos is an array of objects

   oBrx:SetArray( oRecto:aTitulos, .T. ,6, {5,6,3,9,10,21,22,8,16,23,27} )  

   xBrowseStyle(oBrx)   
   oBrx:nDataType := DATATYPE_ARRAY
    
   oBrx:bKeyDown := {| nKey, nFlags | KeyDownDlg(nKey, nFlags)}
   oBrx:bLDblClick := { || BtnClickMarcar() }    //---------------> Duplo click
   oBrx:bClrStd := { ||{CLR_BLACK,ClrPaneBrw()}} //---------------> Mudar cor de fundo


   oBrx:aCols[ 01 ]:cHeader := "Data emissao"
   oBrx:aCols[ 01 ]:nDataStrAlign := AL_CENTER
   oBrx:aCols[ 01 ]:nHeadStrAlign := AL_CENTER

   oBrx:aCols[ 02 ]:cHeader := "Data vencimento"
   oBrx:aCols[ 02 ]:nDataStrAlign := AL_CENTER
   oBrx:aCols[ 02 ]:nHeadStrAlign := AL_CENTER

   oBrx:aCols[ 03 ]:cHeader := "Título"
   oBrx:aCols[ 03 ]:nDataStrAlign := AL_LEFT
   oBrx:aCols[ 03 ]:nHeadStrAlign := AL_LEFT

   oBrx:aCols[ 04 ]:cHeader := "Valor a pagar"
   oBrx:aCols[ 04 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 04 ]:nHeadStrAlign := AL_RIGHT

   oBrx:aCols[ 05 ]:cHeader := "Valor pago"
   oBrx:aCols[ 05 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 05 ]:nHeadStrAlign := AL_RIGHT

   //-- valor original
   oBrx:aCols[ 06 ]:cHeader := "Vr. original"
   oBrx:aCols[ 06 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 06 ]:nHeadStrAlign := AL_RIGHT

   //-- valor de devolução
   oBrx:aCols[ 07 ]:cHeader := "Vr. devolvido"
   oBrx:aCols[ 07 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 07 ]:nHeadStrAlign := AL_RIGHT

   oBrx:aCols[ 08 ]:cHeader := "Retirado"
   oBrx:aCols[ 08 ]:nDataStrAlign := AL_LEFT
   oBrx:aCols[ 08 ]:nHeadStrAlign := AL_LEFT
   oBrx:aCols[ 08 ]:nWidth        := 170  

   //-- histórico.
   oBrx:aCols[ 09 ]:cHeader := "Histórico"
   oBrx:aCols[ 09 ]:nDataStrAlign := AL_LEFT
   oBrx:aCols[ 09 ]:nHeadStrAlign := AL_LEFT

   oBrx:aCols[ 09 ]:nWidth        := 200  

   //-- valor já recebido
   oBrx:aCols[ 10 ]:cHeader := "Vr. já pago"
   oBrx:aCols[ 10 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 10 ]:nHeadStrAlign := AL_RIGHT
   
   //-- data do recebimento parcial
   oBrx:aCols[ 11 ]:cHeader := "Dt. parcial"
   oBrx:aCols[ 11 ]:nDataStrAlign := AL_RIGHT
   oBrx:aCols[ 11 ]:nHeadStrAlign := AL_RIGHT
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: xBrowse autosort 10.6 with object
Posted: Fri Mar 23, 2012 01:30 AM

You may assign a codeblock for each column
oCol:cSortOrder := { |o| YourSortLogic( o ) }

My sample illustrates the usage. It is upto you whether you like to use data names or data ordinal positions to fetch the values for sorting.

Regards



G. N. Rao.

Hyderabad, India
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: xBrowse autosort 10.6 with object
Posted: Fri Mar 23, 2012 11:54 AM
If is up to me, it isn't autosort.

8.02 works fine.
I think the error is here:

Code (fw): Select all Collapse
METHOD SortArrayData()  CLASS TXBrwColumn

   local aCols
   local cOrder
   local nAt, nFor, nLen
   local uSave, cType

   aCols  := ::oBrw:aCols
   cOrder := ::cOrder
   nLen   := Len( aCols )
   nAt    := If( ValType( ::cSortOrder ) == 'N', ::cSortOrder, ::nArrayCol )

   if Len( ::oBrw:aArrayData ) > 0

      for nFor := 1 to nLen
         if aCols[ nFor ]:nArrayCol != ::nArrayCol
            aCols[ nFor ]:cOrder := ""
         endif
      next

      uSave    := ::oBrw:aArrayData[ ::oBrw:nArrayAt ]
      if cOrder == 'A'
         ::oBrw:aArrayData := Asort( ::oBrw:aArrayData,,, {|x,y| ACompare( x, y, nAt, ::lCaseSensitive ) < 0 } )
         ::cOrder     := 'D'
      else
         ::oBrw:aArrayData := Asort( ::oBrw:aArrayData,,, {|x,y| ACompare( x, y, nAt, ::lCaseSensitive ) > 0 } )
         ::cOrder     := 'A'
      endif
      ::oBrw:nArrayAt   := AScan( ::oBrw:aArrayData, uSave )
      ::oBrw:Refresh()

   endif

return self

//---------------------------------------------------------------------------//

static function ACompare( x, y, nAt, lCaseSensitive )

   local nRet := -1
   local xVal, yVal, xType, yType
   local aType := 'ULNPDCHO'

   if ValType( x ) != 'A'
      x     := { x }
   endif
   xVal     := If( nAt <= Len( x ), x[ nAt ], nil )
   xType    := ValType( xVal )

   if ValType( y ) != 'A'
      y     := { y }
   endif
   yVal     := If( nAt <= Len( y ), y[ nAt ], nil )
   yType    := ValType( yVal )

   if xType == yType .and. xType $ 'CDLN'
      if xType == 'C' .and. ! lCaseSensitive
         xVal  := Upper( xVal )
         yVal  := Upper( yVal )
      endif
   else
      xVal  := At( xType, aType )
      yVal  := At( yType, aType )
   endif

   nRet  := If( xVal == yVal, 0, If( xVal < yVal, 1, -1 ) )
//-- xVal and yVal is always numeric 1
return nRet
Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2
Posts: 389
Joined: Mon Oct 13, 2008 11:26 AM
Re: xBrowse autosort 10.6 with object
Posted: Mon Mar 26, 2012 12:31 PM

Any tips?

Email: SamirSSabreu@gmail.com
xHarbour 1.2.3 + Fwhh 20.2

Continue the discussion