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.
Sorting strings in a NON logical way
- Marc Venken
- Posts: 1481
- Joined: Tue Jun 14, 2016 7:51 am
- Location: Belgium
Sorting strings in a NON logical way
Marc Venken
Using: FWH 23.08 with Harbour
Using: FWH 23.08 with Harbour
- Antonio Linares
- Site Admin
- Posts: 42268
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Contact:
Re: Sorting strings in a NON logical way
Dear Marc,
If we can take all the fields values to an array then we can use ASort() with a codeblock
If we can take all the fields values to an array then we can use ASort() with a codeblock
- Marc Venken
- Posts: 1481
- Joined: Tue Jun 14, 2016 7:51 am
- Location: Belgium
Re: Sorting strings in a NON logical way
Thanks.
With that idea, chatGpt gave this solution.
With that idea, chatGpt gave this solution.
Code: Select all | Expand
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.08 with Harbour
Using: FWH 23.08 with Harbour
- nageswaragunupudi
- Posts: 10691
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Contact:
Re: Sorting strings in a NON logical way
The result isMarc Venken wrote:Thanks.
With that idea, chatGpt gave this solution.
Code: Select all | Expand
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
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
G. N. Rao.
Hyderabad, India
- nageswaragunupudi
- Posts: 10691
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Contact:
Re: Sorting strings in a NON logical way
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:
These values correspond the sizes:
You want to read the values in this order: (in other words sort these values )
for further processing.
Is my understanding correct?
If so we can do like this:
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
If your requirement is something different, please let me know.
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
"51,52,53,54,55,56,57,58,59 "
Code: Select all | Expand
"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
"XS,S,M,L,XL,XXL,XXXL,4XL,5XL"
Is my understanding correct?
If so we can do like this:
Code: Select all | Expand
#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
If you want to save the values in a different field in a dbf, you can do
Code: Select all | Expand
FIELD->NEWSIZE := FW_ArrayAsList( aRead )
Regards
G. N. Rao.
Hyderabad, India
G. N. Rao.
Hyderabad, India
- Marc Venken
- Posts: 1481
- Joined: Tue Jun 14, 2016 7:51 am
- Location: Belgium
Re: Sorting strings in a NON logical way
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
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.08 with Harbour
Using: FWH 23.08 with Harbour