Ah, now i understand what you mean by 'sort a string'

Here's a little improovment...
With some Memory routines, you can avoid all those PeekS/PokeS,
and all the For/Next Loops.
The trick is to copy the whole string to an array of bytes, so each
byte holds one string, then you can soret that, and copy the
string back.
lstrlen_(pVar) is a WinAPI command, if you have the Demo, this will
not work, use the commented line below in this case.
Code: Select all
Procedure CharSort(pVar, lNoDup)
Protected nLenS.l
nLenS = lstrlen_(pVar) ; get length: API version, string doesn't have to be peeked
; nLenS = Len(PeekS(pVer)) ; PB version, PeekS needed.
Dim aSort.b(nLenS-1) ; define Array of bytes, so each byte holds one character
; nLenS-1 because PB Dims from 0 to the given boundary, making exactly
; nLenS+1 members, and then this method doesn't work, because there
; will be one byte holding 0, which will be sorted to the front,
; resulting in an empty string then.
CopyMemory(pVar, @aSort(), nLenS) ; fill array with string characters
SortArray(aSort(), 0) ; sort array
CopyMemory(@aSort(), pVar, nLenS) ; copy sorted string back to source
EndProcedure
; test procedure...
cNum.s = "PUREBASIC TEST"
Debug cNum
CharSort(@cNum, 0)
Debug cNum
btw: what is the ' lNoDup' parameter of the procedure for?
Timo