Andrew Truckle
Andrew Truckle

Reputation: 19207

My Pascal InsertionSort of an array of strings is not working correctly

I have seen several questions about Insertion Sort with Pascal. So I thought I would try it out with an array strings.

The text values:

Advanced IF Syntax 
Advanced LOOP Syntax 
ALIGN 
BANNER / NOBANNER 
BLANK 
COLOR / COLOUR 
CONG_MEET_TIME 
DATE 
ENDPAGE 
EOL 
FIELD 
FONT 
HIGHLIGHT / NOHIGHLIGHT 
IF 
IMAGE 
INCLUDE 
LINESPACING 
LOOKUP_FIELD 
LOOP 
LOOP AS 
LOOP SQL 
PARAGRAPH / TEXT 
REQUIRES 
Script Command Syntax 
SEPARATOR 
TAB 
TAB SET 
TALK_THEME 

Here is the code:

Procedure InsertionSort(aTopics : Array of String; size : Integer);
Var
    i, j : Integer;
    caption, topic_id : String;

Begin
    For i := 2 to size-1 do
    Begin
        topic_id := aTopics[i];
        caption := HndTopics.GetTopicCaption(aTopics[i]);
        
        j := i;
        While ((j > 1) AND (CompareText(HndTopics.GetTopicCaption(aTopics[j-1]), caption) > 0)) do
        Begin
            aTopics[j] := aTopics[j-1];
            j := j - 1;
        End;
        aTopics[j] := topic_id;
    End;
End;

It seems to be working 99% correct with one exception. Go to the help documentation and click on the Index tab. Click on the Custom Script Command Syntax item and a pop-up will display:

Popup Window

Why is the first item TALK_THEME? That should be at the end of the list, not the start.

Upvotes: 0

Views: 145

Answers (1)

Jens
Jens

Reputation: 133

successfully tested with HelpNDoc 8.0.0.187 with/in the Script-Editor ...

// ------------------------------------------------------------
// @brief This piece of code create a TStringList,
//        add some text line (animal - items) to the list,
//        sort the list, and
//        display the sored list in a message box on screen.
// ------------------------------------------------------------
procedure sortTest;
var sortedList: TStringList;
begin
  // create instance of TStringList
  sortedList := TStringList.Create;
  try
    // add text (items) line per line:
    // (same as: sortedList.Add('animal');
    sortedList.Text :=
    'Zebra'   + #10 + 'Monkey' + #10 +
    'Hamster' + #10 + 'Lion'   + #10 + 'Cat';
    
    sortedList.Add('Mouse');
    sortedList.Add('Dog');
    
    sortedList.sort;   // sort the items in list
    
    // display sorted list
    ShowMessage(sortedList.Text);
  finally
    sortedList.Clear;  // clear the list (items)
    sortedList.Free;   // free allocated memory
  end;  
end;

// global scope/entry point
begin
  sortTest;
end.

Upvotes: 1

Related Questions