Sorting strings in a NON logical way

Sorting strings in a NON logical way

Postby Marc Venken » Mon Nov 25, 2024 3:16 pm

I have a dbf field that contains data for sizes of garment

"L,M,S,XL,XXL,4XL,5XL,XS,XXXL"
"M,L,S,XL"

the correct sort order would be

"XS,S,M,L,XL,XXXL,4XL,5XL"
"S,M,L,XL"

The sortoptions to compare with :

"XS,S,M,L,XL,XXXL,3XL,4XL,5XL"

Now I think of making a for/next and see where I end, but maybe there is again a 1 or 2 liner to do it.
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1446
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Sorting strings in a NON logical way

Postby Antonio Linares » Mon Nov 25, 2024 4:46 pm

Dear Marc,

If we can take all the fields values to an array then we can use ASort() with a codeblock
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42125
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Sorting strings in a NON logical way

Postby Marc Venken » Mon Nov 25, 2024 7:09 pm

Thanks.

With that idea, chatGpt gave this solution.

Code: Select all  Expand view  RUN

STATIC FUNCTION Test()
local aArray1, aOrder

aArray1 := { "2X", "L", "M", "S", "3X" }

// Definieer een aangepaste volgorde in een array
aOrder := { "S", "M", "L", "XL" , "2X" , "3X" }

// Sorteer aArray1 op basis van de volgorde in aOrder
ASort( aArray1, , , { |x, y| AScan(aOrder, x) < AScan(aOrder, y) } )

xbrowser(aArray1)


RETURN Nil

 
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1446
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium

Re: Sorting strings in a NON logical way

Postby nageswaragunupudi » Mon Nov 25, 2024 11:30 pm

Marc Venken wrote:Thanks.

With that idea, chatGpt gave this solution.

Code: Select all  Expand view  RUN

STATIC FUNCTION Test()
local aArray1, aOrder

aArray1 := { "2X", "L", "M", "S", "3X" }

// Definieer een aangepaste volgorde in een array
aOrder := { "S", "M", "L", "XL" , "2X" , "3X" }

// Sorteer aArray1 op basis van de volgorde in aOrder
ASort( aArray1, , , { |x, y| AScan(aOrder, x) < AScan(aOrder, y) } )

xbrowser(aArray1)


RETURN Nil

 


The result is
aArray1 := {"S","M","L","2X","3X"},
which is exactly the same as aArray1 we started with.
What did we achieve?
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10659
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Sorting strings in a NON logical way

Postby nageswaragunupudi » Tue Nov 26, 2024 4:20 am

Let me first make sure if I understood you requirements properly.

You have a field in your dbf where different sizes are recorded as a list of numeric values.
For example, like this:
Code: Select all  Expand view  RUN
"51,52,53,54,55,56,57,58,59      "

These values correspond the sizes:
Code: Select all  Expand view  RUN
"L,M,S,XL,XXL,4XL,5XL,XS,XXXL"

You want to read the values in this order: (in other words sort these values )
Code: Select all  Expand view  RUN
"XS,S,M,L,XL,XXL,XXXL,4XL,5XL"

for further processing.
Is my understanding correct?

If so we can do like this:
Code: Select all  Expand view  RUN
#include "fivewin.ch"

function Main()

   local cFieldOrder := "L,M,S,XL,XXL,4XL,5XL,XS,XXXL"
   local cReadOrder  := "XS,S,M,L,XL,XXL,XXXL,4XL,5XL"
   local aFieldOrder := HB_ATokens( cFieldOrder, "," )
   local aReadOrder  := HB_ATokens( cReadOrder,  "," )
   local cFieldValue := "51,52,53,54,55,56,57,58,59      "
   local aRead

   AEval( aReadOrder, { |u,i| aReadOrder[ i ] := AScan( aFieldOrder, u ) } )

   aRead := ReadValues( cFieldValue, aReadOrder )

   ? aRead   // -> {"58","53","52","51","54","55","59","56","57"}
   ? FW_ArrayAsList( aRead ) // -> "58,53,52,51,54,55,59,56,57"


return nil

function ReadValues( cFieldValue, aReadOrder )

   local aValue   := HB_ATokens( TRIM( cFieldValue ), "," )
   local aRead    := Array( Len( aReadOrder ) )

   AEval( aRead, { |u,i| aRead[ i ] := aValue[ aReadOrder[ i ] ] } )

return aRead
 

You can do anything with the values read in the order you want.
If you want to save the values in a different field in a dbf, you can do
Code: Select all  Expand view  RUN
FIELD->NEWSIZE := FW_ArrayAsList( aRead )


If your requirement is something different, please let me know.
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10659
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Sorting strings in a NON logical way

Postby Marc Venken » Tue Nov 26, 2024 11:10 am

Mr. Rao,

You look a different way...

My dbf field will hold data like this (sizes)

cData = "XL,S,M,2X,3X" // or any combination like ( "S,L,M")

Make a array from cData (I can)

aArray1 := { "XL", "S", "M", "2X", "3X" }

aOrder := { "XS" ,"S", "M", "L", "XL" , "2X" , "3X", "4X" } // All posible options AND also in the order I would like them.

ASort( aArray1, , , { |x, y| AScan(aOrder, x) < AScan(aOrder, y) } )

Result =

aArray1 := { "S", "M", "XL", "2X", "3X" }

// save result back to dbf with correct functions.. (=ok)

I was not able to use the correct aSort, because I did not know the correct codeblock.

This is working for my project.

Thanks for looking into it
Marc Venken
Using: FWH 23.04 with Harbour
User avatar
Marc Venken
 
Posts: 1446
Joined: Tue Jun 14, 2016 7:51 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 46 guests