Sort array but not typical from A-Z or Z-A

Post Reply
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Sort array but not typical from A-Z or Z-A

Post by Marc Venken »

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 20.08 with Harbour
User avatar
Otto
Posts: 4470
Joined: Fri Oct 07, 2005 7:07 pm
Contact:

Re: Sort array but not typical from A-Z or Z-A

Post by Otto »

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.
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org

********************************************************************
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Sort array but not typical from A-Z or Z-A

Post by Marc Venken »

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 20.08 with Harbour
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Sort array but not typical from A-Z or Z-A

Post by nageswaragunupudi »

Code: Select all

#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
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Sort array but not typical from A-Z or Z-A

Post by Marc Venken »

Super !! Works as wanted
Marc Venken
Using: FWH 20.08 with Harbour
Post Reply