Data sorting with LB/JB

Liberty BASIC has a built-in abilty to sort. It seems little known or used. I thought I'd play a bit with it, because two separate LB contacts have had tasks to do that involved sorting. For these sizes of dataset LB is very useful. However from choice if I didn't want LB's ability to present results via a GUI so easily I'd probably import the data into a spreadsheet... or a database and use SQL.

I located a file of 500 entries by 11 columns of fictional but realistic data. It was in csv format, with every item surrounded by chr$( 34) 'quote signs'. And there was a first row of column headers which I didn't bother removing. The column formatting has not been adjusted for the different length of entries in some fields.

The code below demonstrates how quick the LB sort is. But only if you are sorting on only one column. It shows all the data as-read, then sorts it by in turn column 1, 2 and 9, printing only selected columns each time. Of course once the data is in an array all sorts of further work is possible- correlations; numerical analyses; conditionals; etc.

You could easily re-save the data in sorted form, or with only certain columns. And you could do a secondary sort on another column by isolating the start/finish of records sorted on one column and sharing a value, and re-sort these only on a second chosen column.


LB code


'   NB LB sort is dodgy anyway in a few special cases...
'   AND it sorts on only one column at a time... although rapidly...
'   AND you need to be consistent in using upper/lower case for sorting...
'   AND sort order is unusual.... ( upper/lowercase/numbers/other characters..
'   AND is 0123 before or after 123 or 00123..
'   AND embedded quotes '"' and commas are easily confused...

dim record$( 500, 11)  '   allow for 500 rows of 11 data items
'   I'd find this programmatically by counting CRLFs and separator strings...

open "uk-500.csv" for input as #inFile
    for entry =1 to 500
        line input #inFile, row$
        for column =1 to 11
            item$ =word$( row$, column, chr$( 34) +"," +chr$( 34))
            '   I use "," as the three-character separator and strip off " at beg'g of col 1 and end of col 11.
            if column = 1 then item$ =mid$(  item$, 2)
            if column =11 then item$ =left$( item$, len( item$) -1)
            record$( entry, column) =item$
        next column
    next entry
close #inFile

print: print "Data as received.."
for entry =1 to 500
    for column =1 to 11
        print record$( entry, column),
    next column
    print
next entry

    print: print "Data sorted on column 1"
    sort record$(), 1, 500, 1
    for entry =1 to 500
        print           record$( entry, 1);
        print tab( 30); record$( entry, 2);
        print tab( 60); record$( entry, 9)
    next entry
    print

    print: print "Data sorted on column 2"
    sort record$(), 1, 500, 2
    for entry =1 to 500
        print           record$( entry, 1);
        print tab( 30); record$( entry, 2);
        print tab( 60); record$( entry, 9)
    next entry
    print

    print: print "Data sorted on column 9"
    sort record$(), 1, 500, 9
    for entry =1 to 500
        print           record$( entry, 1);
        print tab( 30); record$( entry, 2);
        print tab( 60); record$( entry, 9)
    next entry
    print


end