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: 1436
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: 42107
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: 1436
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: 10645
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 58 guests