SORT fails to provide "dictionary" sorting

From Liberty BASIC Family
Jump to navigation Jump to search


Description

When sorting a string array, SORT command does not respect the character's case and so fails to provide a similar level of comparison to the > or < operators. Liberty offers alphabetic comparison and compares characters and strings in "Dictionary" order. SORT should therefor sort "Abc" ahead of "abc".

Also , second sorting , creates a different order.

Sample output from example program:

Characters sorted
  ' -   ! " # $ % & ( ) * , . /
: ; ? @ [ \ ] ^ _ ` { | } ~ + <
= > 0 1 2 3 4 5 6 7 8 9 a A b B
C c D d e E F f g G h H i I j J
k K l L M m n N O o p P q Q r R
S s t T u U v V w W X x y Y z Z

Characters sorted another time - note differences
  ' -   ! " # $ % & ( ) * , . /
: ; ? @ [ \ ] ^ _ ` { | } ~ + <
= > 0 1 2 3 4 5 6 7 8 9 a A B b
c C D d e E f F g G h H I i j J
k K L l M m N n o O p P q Q R r
S s T t U u v V W w X x y Y z Z

Now, see the differences - marked with (!)
  ' -   ! " # $ % & ( ) * , . /
: ; ? @ [ \ ] ^ _ ` { | } ~ + <
= > 0 1 2 3 4 5 6 7 8 9 a A B!b!
c!C!D d e E f!F!g G h H I!i!j J
k K L!l!M m N!n!o!O!p P q Q R!r!
S s T!t!U!u!v V W!w!X x y Y z Z

Example code to demonstrate the bug.

n = 127

DIM a$(n)
DIM b$(n)

FOR i = 1 TO n
    a$(i) = CHR$(i)
NEXT

'Sort by sort routine
print "Characters sorted"
sort a$() ,32,127
gosub [printTable]

'save previous sort results
FOR i = 1 TO n
    b$(i)=a$(i)
NEXT

'sort one another time - note output is different
print "Characters sorted another time - note differences"
sort a$() ,32,127
gosub [printTable]

print "Now, see the differences - marked with (!)"
gosub [printDiffTable]

END

[printTable]
    FOR i = 2 TO int(n/16)
        FOR j = 0 TO 15
            c = j + i * 16
            PRINT a$(c); " ";
        NEXT
        PRINT
    NEXT
    PRINT
RETURN

[printDiffTable]
    FOR i = 2 TO int(n/16)
        FOR j = 0 TO 15
            c = j + i * 16
            PRINT a$(c); word$("!, ",(a$(c)=b$(c))+1,",");
        NEXT
        PRINT
    NEXT
    PRINT
RETURN

Example code to work around the bug.

use your own sort routine?

'code to work around the bug goes here