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


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 56 guests