'sort the file records.txt containing several columns
'separated by comma

rec$ = file.read$("/records.txt")
column = 1   'column to be sorted

'convert the text file into an array$
dim record$(100) 'lets say max 100 lines

'extract the text line by line into an array
pos_start = 1
i = 0
do
  pos_end = instr(pos_start, rec$, chr$(10))
  if (pos_end <> 0)
    record$(i) = mid$(rec$, pos_start, pos_end - pos_start - 1)
  else
   record$(i) = mid$(rec$, pos_start)
  end if
  pos_start = pos_end + 1
  'print record$(i)
  i = i + 1
loop while (pos_end <> 0)
num_records = i

'extract the given column
dim sort_elements$(num_records)
' sort using the given column
for i = 0 to num_records 
  sort_elements$(i) = trim$(word$(record$(i), column, ","))
next i

'now bubble sort
i = 0
j = 0
for i=0 to num_records-1
for j=i to num_records-1
  if sort_elements$(i) > sort_elements$(j) then 
     a$ = sort_elements$(i)
     sort_elements$(i) = sort_elements$(j)
     sort_elements$(j) = a$
     'reverse the initial line with all the elements
     a$ = record$(i)
     record$(i) = record$(j)
     record$(j) = a$     
  end if
next j
next i
print "sorted lines:"
dim sort_elements$(0) ' clear the sort_element$ not required anymore (clear memory)
for z = 0 to num_records-1
  print record$(z)
next z


esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
matrix1:V+
matrix1:GND
matrix1:DIN
matrix1:CS
matrix1:CLK
matrix1:V+.2
matrix1:GND.2
matrix1:DOUT
matrix1:CS.2
matrix1:CLK.2