Page 1 of 1
A simple sort problem?
Posted: Wed Jun 02, 2021 9:06 am
by collectordave
I have a list of strings in the form
1
2A
2
3
20
The numeric parts can be up to 99999 and the alpha part can be any letter a to z
The list can be in any order and I need to sort the list so the desired output would be:-
1
2
2A
3
20
tried this:-
Code: Select all
Global NewList MyStrings.s()
AddElement(MyStrings())
MyStrings() = "1"
AddElement(MyStrings())
MyStrings() = "2A"
AddElement(MyStrings())
MyStrings() = "2"
AddElement(MyStrings())
MyStrings() = "3"
AddElement(MyStrings())
MyStrings() = "20"
ForEach MyStrings()
Debug MyStrings()
Next
Debug ""
SortList(MyStrings(),#PB_Sort_Ascending)
ForEach MyStrings()
Debug MyStrings()
Next
Is there any way to sort this list?
Regards
CD
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 9:27 am
by Keya
Im guessing the existing sort routines won't work specifically for how you need them.
How about creating a duplicate array, but modifying them so that, for example...
1 = 10000
2 = 20000
2A = 20256 (A = +256.... B = +257 etc) (or just +use their Asc() values)
Please forgive me, my numbers are off, but I hope you get the general idea
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 9:36 am
by Marc56us
Interesting problem
I thought I had a solution, but no, it doesn't work because the sort order is not "logical".
I give my solution anyway in case it gives ideas to the following ones ?
(extract last char, convert to string, fill with left "0", then remove)
The numeric parts can be up to 99999 and the alpha part can be any letter a to z
Code: Select all
Global NewList MyStrings.s()
AddElement(MyStrings()) : MyStrings() = "1"
AddElement(MyStrings()) : MyStrings() = "2A"
AddElement(MyStrings()) : MyStrings() = "2"
AddElement(MyStrings()) : MyStrings() = "3"
AddElement(MyStrings()) : MyStrings() = "20"
ForEach MyStrings()
Debug MyStrings()
Next
ForEach MyStrings()
Letter$ = Right(MyStrings(), 1)
Number$ = RemoveString(MyStrings(), Letter$)
MyStrings() = LTrim(RSet(Number$, 5, "0") + Letter$, "0")
Next
Debug ""
SortList(MyStrings(),#PB_Sort_Ascending)
ForEach MyStrings()
Debug MyStrings()
Next
Result:
Expected:
Actually I think I forgot to test that the last character is a letter before removing it, so the eventual zero goes too

Re: A simple sort problem?
Posted: Wed Jun 02, 2021 9:50 am
by collectordave
Thanks for the replies.
Given me an idea.
If I pack the number with leading zeros so that
1 becomes 00001
and 20 becomes 00020 etc
it seems to work
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 10:27 am
by collectordave
Yes
got it working.
Pack with leading zeros and add on the alpha character.
Sort the list
Then remove the leading zeros.
Code: Select all
ForEach MyStrings()
Alpha.s = Right(MyStrings(),Len(MyStrings()) - Len(Str(Val(MyStrings()))))
MyStrings() = RSet(Str(Val(MyStrings())),5,"0") + Alpha
Next
SortList(MyStrings(),#PB_Sort_Ascending)
ForEach MyStrings()
For iLoop = 1 To 5
If Left(MyStrings(),1) = "0"
MyStrings() = Right(MyStrings(),Len(MyStrings()) -1)
Else
Break
EndIf
Next
Debug MyStrings()
Next
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 10:45 am
by Keya
glad to hear you've got it going

I would still opt for an integer version over string manipulation though (though maybe it's not worth it)
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 2:09 pm
by eck49
@collectordave
Good solution.
If at some point you need to extend the alpha part you could also pack on the right and your choice of packing could dictate the order of the strings
2A
2AA
2AC
2B
2DB
as you may want all single letters to come before double letters (cf usual spreadsheet column label notation) or just alphabetical like the above.
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 4:45 pm
by glomph
Leading zeros are not necessary, spaces work too. To get rid of them, just use Trim().
Re: A simple sort problem?
Posted: Wed Jun 02, 2021 8:43 pm
by Marc56us
Simplify the end
Code: Select all
ForEach MyStrings()
; For iLoop = 1 To 5
; If Left(MyStrings(),1) = "0"
; MyStrings() = Right(MyStrings(),Len(MyStrings()) -1)
; Else
; Break
; EndIf
; Next
Debug LTrim(MyStrings(), "0")
Next
