FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Sort array but not typical from A-Z or Z-A
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Sort array but not typical from A-Z or Z-A
Posted: Mon Aug 31, 2020 06:49 PM

Helllo,

I need to sort lots of datalines not a typical way...

I get the charStrings like

cVar = "S,M,XL,L,2X,3X" this is comming from a loop reading a database

I put them in a array, but then the array should sort always with this as possible result.

aResult = "S,M,L,XL,2X,3X" it is possible that cVar has only 3 items cVar = "M,S,XL" and aResult should be "S,M,XL"

"XS,S,M,L,XL,2X,3X,4X,5X" are the options that can be found...

How can it be done ? Asort = different

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Sort array but not typical from A-Z or Z-A
Posted: Mon Aug 31, 2020 07:32 PM

Marc, couldn't you insert an additional field.
Or a function "XS, S, M, L, XL, 2X, 3X, 4X, 5X" where you replace XS with A, S with B, etc and then sort and then reset.

Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Sort array but not typical from A-Z or Z-A
Posted: Mon Aug 31, 2020 07:35 PM
Otto wrote:Marc, couldn't you insert an additional field.
Or a function "XS, S, M, L, XL, 2X, 3X, 4X, 5X" where you replace XS with A, S with B, etc and then sort and then reset.


Good idea...
Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Sort array but not typical from A-Z or Z-A
Posted: Tue Sep 01, 2020 12:10 PM
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main()

   local oDlg, oFont
   local cVar  := PadR( "S,M,XL,L,2X,3X", 25 )

   DEFINE FONT oFont NAME "VERDANA" SIZE 0,-18
   DEFINE DIALOG oDlg SIZE 400,200 PIXEL TRUEPIXEL FONT oFont

   @  20, 20 SAY "Text   :" SIZE 70,26 PIXEL OF oDlg
   @  70, 30 SAY "Result :" SIZE 70,26 PIXEL OF oDlg

   @  20,100 GET cVar PICTURE "@!" SIZE 280,30 PIXEL OF oDlg VALID ( oDlg:Update(), .T. )
   @  70,100 SAY { || MySortFunc( cVar ) } SIZE 280,24 PIXEL OF oDlg UPDATE

   @ 120,280 BUTTON "CLOSE" SIZE 100,30 PIXEL OF oDlg ACTION oDlg:End()

   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont

return nil

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

function MySortFunc( cVar )

   local cOrder := ",XS,S,M,L,XL,2X,3X,4X,5X,"
   local aVar, cRet

   aVar  := FW_ListAsArray( CharRem( " ", AllTrim( cVar ) ) )
   ASort( aVar, nil, nil, { |x,y| At( "," + x + ",", cOrder  ) < At( "," + y + ",", cOrder ) } )

return FW_ArrayAsList( aVar )

//----------------------------------------------------------------------------//
Regards



G. N. Rao.

Hyderabad, India
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Sort array but not typical from A-Z or Z-A
Posted: Tue Sep 01, 2020 03:28 PM

Super !! Works as wanted

Marc Venken

Using: FWH 23.08 with Harbour

Continue the discussion