CHARSORT()

Sort sequences within a string.

Syntax

CHARSORT (<[@]cString>, [<nElementLength>], [<nCompareLength>],
[<nIgnoreCharacters>], [<nElemenOffset>], [<nSortLength>],
[<lDescending>]) -> cSortedString

Arguments

<[@]cString> is the string that should be processed [<nElementLength>] specifies the length of the elements that should be sorted Default: 1 [<nCompareLength>] specifies how many characters within one element should be used for comparison Default: <nElementLength> [<nIgnoreCharacters>] specifies the number of characters at the beginning of <cString> that should be ignored in the sort process Default: 0 [<nElementOffset>] specifies the offset of the comparison string within a element Default: 0 [<nSortLength>] specifies how many characters in <cString>, starting from the <nIgnoreCharacters> position, should be sorted Default: len(cString)-nIgnoreCharacters [<lDescending>]) specifies whether the process should sort descending or not

Returns

<cSortedString> the string resulting from the sort process

Description

The CHARSORT function sorts the characters within a string <cString>. With the parameters <nIgnoreCharacters> and <nSortLength>, you can determine that only the substring from position <nIgnoreCharacters>+1 to position <nIgnoreCharacters>+<nSortLength> within <cString> should be sorted. The sorting algorithm is determined with the other parameters. <nElementLength> specifies the length of one element, i.e. there are <nSortLength>/<nElementLength> elements that are sorted. Note that surplus characters are not sorted but stay at their position. To do the sorting, the function uses the Quicksort algorithm implemented in the C-lib qsort() function. This algorithm needs to know how to compare and order two elements. This is done by comparing the ASCII values of a substring within each element. This substring is determined by the parameters <nElementOffset> and <nCompareLength> and the order by <lDescending>. By setting the CSETREF() switch to .T., one can omit the return value of the function, but one must then pass <cString> by reference.
Examples
      ? CHARSORT("qwert")                     // "eqrtw"
      ? CHARSORT("qwert", 2)                  // "erqwt"
      ? CHARSORT("b1a4a3a2a1", 2, 1)          // "a2a1a3a4b1"
      ? CHARSORT("XXXqwert", 1, 1, 3)         // "XXXeqrtw"
      ? CHARSORT("b1a4a3a2a1", 2, 1, 0, 1)    // "a1b1a2a3a4"
      ? CHARSORT("384172852", 1, 1, 0, 0, 4)  // "134872852"
      ? CHARSORT("qwert", .T.)                // "wtrqe"
Tests
      CHARSORT("qwert")                     == "eqrtw"
      CHARSORT("qwert", 2)                  == "erqwt"
      CHARSORT("b1a4a3a2a1", 2, 1)          == "a2a1a3a4b1"
      CHARSORT("XXXqwert", 1, 1, 3)         == "XXXeqrtw"
      CHARSORT("b1a4a3a2a1", 2, 1, 0, 1)    == "a1b1a2a3a4"
      CHARSORT("384172852", 1, 1, 0, 0, 4)  == "134872852"
      CHARSORT("qwert", .T.)                == "wtrqe"
Status

Ready

Compliance

CHARSORT() is compatible with CT3's CHARSORT().

Platforms

All

Files

Source is charsort.c, library is ct3.

See Also