ASCII_string_compare

=ASCII string compare function= As you should know, JB sorts strings in order, different from ASCII. [|BuildingBlocks::String Comparison] It is basically OK, after you've been warned about it. ;)

But just in case you think you really need sort in ASCII order - here is ASCII compare function that could be used for ASCII sort. And usage demo. Demo uses bubble sort - if you need it faster, there is a qsort example in JB folder.

code format="lb" 'ASCII string compare function and usage demo 'returns -1 0 1 as C strcmp

'some demo text a$ = "Thanks for trying Liberty BASIC v4.01, the easiest programming" _ + " language around since 1992! Liberty BASIC has been featured by McGraw" _ + " Hill and Ziff Davis, and also in a tutorial series in PC Plus magazine!" _ + " Thousands of people use Liberty BASIC every day to write useful" _ + " software, create software for sale, create games, and teach and learn" _ + " programming."

'count words i = 0 c$ = "*" while c$<>"" i = i+1 c$=word$(a$, i) '   print i, c$ wend

n = i-1 'dim and fill array. Every other word made uppercase dim w$(n) for i = 1 to n   if i mod 2 then w$(i)=upper$(word$(a$, i)) else w$(i)=word$(a$, i)   end if next

print "***************************************************" print "Initial array" for i = 1 to n   print i, w$(i) next

'bubble sort for i = n-1 to 1 step -1 for j = 1 to i       if w$(j)>w$(j+1) then t$=w$(j):w$(j)=w$(j+1):w$(j+1)=t$ end if   next next

print "***************************************************" print "Array after native JB sort" for i = 1 to n   print i, w$(i) next

'bubble sort with AsciiCompare function for i = n-1 to 1 step -1 for j = 1 to i       if AsciiCompare(w$(j),w$(j+1))=1 then t$=w$(j):w$(j)=w$(j+1):w$(j+1)=t$ end if   next next

print "***************************************************" print "Array after ASCII sort" for i = 1 to n   print i, w$(i) next

end

'*************************************************** 'ASCII string compare 'returns -1 0 1 as C strcmp 'Then sorting, condition 'AsciiCompare(w$(j),w$(j+1))=1 is to be used instead of w$(j)>w$(j+1) function AsciiCompare(s1$,s2$) if s1$=s2$ then AsciiCompare=0: exit function minLen=len(s1$) if minLen>len(s2$) then minLen=len(s2$) for i=1 to minLen c1$=mid$(s1$,i,1) c2$=mid$(s2$,i,1) if c1$<>c2$ then   'sign of asc(c1$)-asc(c2$) AsciiCompare = (asc(c1$)>asc(c2$))*2-1 exit function end if   next 'if we are here then one string ended, and we already checked for equal - so   AsciiCompare=(len(s1$)>len(s2$))*2-1 end function code