This shows you the differences between two versions of the page.
sorting_20the_20characters_20in_20a_20string [2018/03/31 13:19] 127.0.0.1 external edit |
sorting_20the_20characters_20in_20a_20string [2018/04/17 18:52] tbest3112 Added syntax highlighting |
||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, October 2008//\\ \\ You can quickly and easily sort the elements of an **array** by using the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwing.html#sortlib|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: \\ \\ | //by Richard Russell, October 2008//\\ \\ You can quickly and easily sort the elements of an **array** by using the [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwing.html#sortlib|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: \\ \\ | ||
+ | <code bb4w> | ||
INSTALL @lib$+"SORTLIB" | INSTALL @lib$+"SORTLIB" | ||
Sort% = FN_sortinit(0,0) | Sort% = FN_sortinit(0,0) | ||
+ | </code> | ||
Change the parameters of **FN_sortinit** if necessary, as explained [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwing.html#sortinit|here]].\\ \\ The following function sorts a string into an ascending (or descending) sequence of characters:\\ \\ | Change the parameters of **FN_sortinit** if necessary, as explained [[http://www.bbcbasic.co.uk/bbcwin/manual/bbcwing.html#sortinit|here]].\\ \\ The following function sorts a string into an ascending (or descending) sequence of characters:\\ \\ | ||
+ | <code bb4w> | ||
DEF FNsortstring(A$) | DEF FNsortstring(A$) | ||
LOCAL C%, A&() | LOCAL C%, A&() | ||
Line 12: | Line 15: | ||
CALL Sort%, A&(0) | CALL Sort%, A&(0) | ||
= $$^A&(0) | = $$^A&(0) | ||
+ | </code> | ||
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 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. |