0

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164

1 Answers1

1

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.
Jens
  • 84
  • 1
  • 10
  • Yes, but if you look at the code I presented it is an array of string. And not a TStringList. – Andrew Truckle Sep 10 '22 at 11:06
  • 1
    a TStringList is a list, okay. But it can be used for "Add" new items, and quickly "sort". I don't knew your needs, but I would present you, that you can use OOP-Style-Pascal-Code to minimize your next coding sources. What you made with my advise is on you. But it's okay, if you use Array. I don't know HelpNDoc < 7, so I can only write about new Version that you can try out/download free for personal/trial usage in new Version 8.1. Have fun ... – Jens Sep 13 '22 at 14:20
  • True. I do have the latest Professional version. I don't think I actually use the manual sorting code anymore as I do not see it in my 7.9 template. They may have fixed the issue and it was no longer needed. Anyway, thanks for the idea which I am sure will come in handy at some point. – Andrew Truckle Sep 13 '22 at 17:03