User Tools

Site Tools


sorting_20the_20characters_20in_20a_20string

This is an old revision of the document!


Sorting the characters in a string

by Richard Russell, October 2008

You can quickly and easily sort the elements of an array by using the SORTLIB library, but BBC BASIC for Windows does not directly provide the facility to sort the characters in a string. The code listed below allows you to do that.

First you should initialise SORTLIB in the usual way, just once at the start of your program:

      INSTALL @lib$+"SORTLIB"
      Sort% = FN_sortinit(0,0)

Change the parameters of FN_sortinit if necessary, as explained here.

The following function sorts a string into an ascending (or descending) sequence of characters:

      DEF FNsortstring(A$)
      LOCAL C%, A&()
      C% = LEN(A$)
      DIM A&(C%)
      $$^A&(0) = A$
      CALL Sort%, A&(0)
      = $$^A&(0)

This works by temporarily creating a byte array, copying the string into the array, sorting the byte array, and finally returning the contents of the array as a string.

This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
sorting_20the_20characters_20in_20a_20string.1522502383.txt.gz · Last modified: 2018/03/31 13:19 by 127.0.0.1