
Post by B+ on Apr 22, 2019 23:40:48 GMT



Post by Rod on Apr 23, 2019 8:08:31 GMT
Sort is doing what it is programmed to do, case insensitive lexographic order. Sorting numbers is easy, sorting text has a few issues, not the least of which is that "A" is worth more than "a" in ASCII money and so gets put in the wrong place! Who invented ASCII? But there is more....
'create list of string values dim list$(16) list$(1)="alice" list$(2)="ali" list$(3)="Ali" list$(4)="Alice" list$(5)="bob" list$(6)="Bob" list$(7)="Ba" list$(8)="Bb" list$(9)="Bc"
size = 9 true=1 false=0 'show the list print "Initial order: " for i = 1 to size print list$(i);" "; next i print print
'Sort(1) case insensitive, lexographic sort list$(,1,9
'show the list print "sort() case insensitive, lexographic order:" for i = 1 to size print list$(i); " "; next i print print
'Sort(2) note previous order is not preserved sort list$(,1,9
'show the list print "sort(2) note previous order is not preserved:" for i = 1 to size print list$(i); " "; next i print print
'LB $<> leading character distortion for i = 1 to size1 min$ = list$(i) pos = i for j = i+1 to size if list$(j) < min$ then pos = j min$ = list$(j) end if next j list$(pos) = list$(i) list$(i) = min$ next i print
'show the list print "String comparison, leading character distortion:" for i = 1 to size print list$(i); " "; next i print print 'Forced lower case ascii order for i = 1 to size1 min$ = list$(i) pos = i for j = i+1 to size if AsciiCompare(list$(j), min$) = true then pos = j min$ = list$(j) end if next j list$(pos) = list$(i) list$(i) = min$ next i print "Forced lower case Ascii order:" for i = 1 to size print list$(i); " "; next i print print
'Pure ascii order for i = 1 to size1 min$ = list$(i) pos = i for j = i+1 to size if Ascii(list$(j), min$) = true then pos = j min$ = list$(j) end if next j list$(pos) = list$(i) list$(i) = min$ next i print "Pure Ascii order:" for i = 1 to size print list$(i); " "; next i print print print "And it all gets worse when we introduce foreign characters" wait
function AsciiCompare(s1$,s2$) l=1 s1$=lower$(s1$) s2$=lower$(s2$) AsciiCompare=1 while l <= len(s1$) and l <= len(s2$) and AsciiCompare=1 if asc(mid$(s1$,l,1)) > asc(mid$(s2$,l,1)) then AsciiCompare=0 l=l+1 wend if len(s1$)>len(s2$) then AsciiCompare=0 end function
function Ascii(s1$,s2$) l=1 AsciiCompare=1 while l <= len(s1$) and l <= len(s2$) and AsciiCompare=1 if asc(mid$(s1$,l,1)) > asc(mid$(s2$,l,1)) then AsciiCompare=0 l=l+1 wend if len(s1$)>len(s2$) then AsciiCompare=0 end function



Post by B+ on Apr 23, 2019 12:42:41 GMT
It's messy alright!
Last night after I posted this, I realized that following strict ANSII values ALL the capitalized words would come before all the lowercase.
Alice Ball Zeb alice ball zeb
Well the lesson here is if you want to use the faster binary search method for finding things in sorted array, you have to use the same compare evaluation for sort routine as the find routine uses to decide if x > y.

