0

I am working with Lazarus. I have two TStringList, after I sort them, I show them to screen. But the order is wrong. I am impossible to determine how it was wrong.

This is screenshot when the TStringList is not sorted:

enter image description here

After I call FMover.AList.Sort and FMover.BList.Sort, I show them to screen. This is screenshot after the lists is sorted.

enter image description here

You can see, item '.../kam14.in' is appeared before '.../kam1.in'. Sort procedure is not working properly.

This is some procedure used in the program.

procedure TAddProblemForm.actRegexLoadExecute(Sender: TObject);
var
  ARegExpr, BRegExpr: TRegExpr;
  s: String;
  AExpr, BExpr: String;
  Form: TRegexOptionForm;
begin
  Form := TRegexOptionForm.Create(Self);

  if Form.Execute('in', 'out') = mrOK then
  begin
    ARegExpr:=TRegExpr.Create;
    BRegExpr:=TRegExpr.Create;
    FMover.Clear;

    ARegExpr.Expression := Form.AExpr;
    BRegExpr.Expression := Form.BExpr;
    AddSearchResult(FMover.List, SearchDir, true);

    for s in FMover.List do
    begin
      if ARegExpr.Exec(s) then
        FMover.AList.Add(s)
      else if BRegExpr.Exec(s) then
        FMover.BList.Add(s);
    end;

    // *******************
    actInvalidate.Execute;
    FMover.AList.Sort;
    FMover.BList.Sort;
    actInvalidate.Execute;
    // *******************

    ARegExpr.Free;
    BRegExpr.Free;
  end;

  Form.Free;
end; 

And another

  procedure TAddProblemForm.actInvalidateExecute(Sender: TObject);
  var 
    s: String;
  begin
    ListBox1.Clear;
    ListBox2.Clear;

    for s in FMover.AList do
      ListBox1.Items.Add(s);

    for s in FMover.BList do
      ListBox2.Items.Add(s);

    Application.ProcessMessages;
  end;   
Delphi Coder
  • 1,723
  • 1
  • 14
  • 25
kien_coi_1997
  • 129
  • 2
  • 14
  • Look at this: http://stackoverflow.com/questions/15257746/custom-sort-method-in-delphi-to-sort-list-of-strings –  Nov 27 '13 at 06:35
  • Thank you very much, I have understand how could it be wrong. But I don't know what does the builtin `Sort` method implemented. Thank you again. – kien_coi_1997 Nov 27 '13 at 11:36
  • The sort method just uses AnsiCompareText (or some similar routine for Unicode) to determine whether a value should come after or before another value. The sort implementation is just a basic quicksort. It is possible to override your value test with another routine if needed, it just needs to return an integer that is +ve if A > B, -ve if A < B or 0 if A = B in sort order terms. – Matt Allwood Nov 27 '13 at 16:33
  • Thanks Matt Allwood for your comment. I have resolved my problem successfully. I 've write a independent method to compare two string. And it worked. – kien_coi_1997 Nov 28 '13 at 01:36

0 Answers0