Sorted Property Example

The following program code demonstrates the effect the Sorted property has on a TMult variable. Notice that setting Sorted back to False does not revert the list to its unsorted order:

  1. Start a new application.

  2. Drop one memo and one button on the form. Arrange controls as in the figure below.

  3. Copy the following code to the Button1.OnClick event:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Mult1: TMult;
      Subscript: string;
    begin
      Mult1 := TMult.Create(Form1);    //Create Mult1. Make Form1 its owner
    
      Mult1['First'] := 'One';         //Fill Mult1 with some strings
      Mult1['Second'] := 'Two';
      Mult1['Third'] := 'Three';
      Mult1['Fourth'] := 'Four';
      Mult1['Fifth'] := 'Five';
    
      Memo1.Font.Name := 'Courier';    //configure memo box for better display
      Memo1.ScrollBars := ssVertical;
      Memo1.Lines.Clear;
      Memo1.Lines.Add('Natural order:');
    
      Subscript := '';                 //set a starting point
      repeat
        Subscript := Mult1.Order(Subscript, 1);  //get next Mult element
        if Subscript <> '' then                  //if not the end of list
          Memo1.Lines.Add(Format('%10s', [Subscript]) + ' - ' + Mult1[Subscript])                  //display subscript - value
      until Subscript = '';            //stop when reached the end
    
      Mult1.Sorted := True;             //list will now be sorted alphabetically
      Memo1.Lines.Add('');
      Memo1.Lines.Add('Sorted order:');
    
      Subscript := '';                 //set a starting point
      repeat
        Subscript := Mult1.Order(Subscript, 1);  //get next Mult element
        if Subscript <> '' then                  //if not the end of list
          Memo1.Lines.Add(Format('%10s', [Subscript]) + ' - ' + Mult1[Subscript])                  //display subscript - value
      until Subscript = '';            //stop when reached the end
    
      Mult1.Sorted := False;           //existing entries will remain in sorted order
      Memo1.Lines.Add('');
      Memo1.Lines.Add('Unsorted order:');
    
      Subscript := '';                 //set a starting point
      repeat
        Subscript := Mult1.Order(Subscript, 1);  //get next Mult element
        if Subscript <> '' then                  //if not the end of list
          Memo1.Lines.Add(Format('%10s', [Subscript]) + ' - ' + Mult1[Subscript])                  //display subscript - value
      until Subscript = '';            //stop when reached the end
    
    end;
  4. Run project and click on the button.

    Expected output:

Sorted Property Example

You may have to scroll up and down to see all of the output.