在Delphi中对备忘录进行排序

时间:2013-10-25 14:00:11

标签: delphi delphi-6 memo

我必须在Delphi 6中为我的学校制作一份高分备忘录。 有没有办法在数字或字母表上对MemoLines进行排序?

我使用4个Tedits和1个TMemo。 如果游戏结束,我的代码将检查谁获得了最高分。 这是如何检查Player1获得的分数是否高于player2:

if in1>p2in1 then begin
  highscore.naammemo.Lines.Add(Speler1.Caption);
  highscore.saldomemo.Lines.Add(Saldo1.Text);
end;

如何为TMemo创建代码以对每个游戏的最高得分进行排序?

2 个答案:

答案 0 :(得分:4)

我认为最简单的方法就是这样:

  1. 将内容从备忘录转移到TStringList实例。
  2. CustomSort实例上调用TStringList,传递适当的排序比较功能。
  3. 将内容传回备忘录。
  4. 步骤1和3是对Assign的简单调用。所以第1步将是:

    StringList.Assign(Memo.Lines);
    

    第3步将是:

    Memo.Lines.Assign(StringList);
    

    第2步是棘手的​​一点。你必须提供这种类型的比较功能:

    TStringListSortCompare = function(List: TStringList; 
      Index1, Index2: Integer): Integer;
    

    您的功能将如下所示:

    function MySortCompare(List: TStringList; Index1, Index2: Integer): Integer;
    begin
      Result := MyCompareStrings(List[Index1], List[Index2]);
    end;
    

    其中MyCompareStrings是一个根据您的规则比较两个字符串的函数。该函数的返回值遵循比较函数的通常约定。负数表示小于,正数表示大于0表示相等。

    当然,如果您愿意,可以将逻辑直接内联到MySortCompare

答案 1 :(得分:3)

以下是一些示例代码,可让您尝试排序。它在每一行上使用文本值和数字,以制表符(#9)分隔。每个按钮单击处理程序的开头都有代码,它将文本重置为相同的起始值,因此您可以看到效果。第一个按钮(btnNameSort)使用标准TStringList.Sort按文本值排序,第二个按钮(btnScoreSort)使用TListSortCompare自定义排序功能按数值排序。

// Simply uses TStringList.Sort to sort in the default (alpha) order
procedure TForm1.btnNameSortClick(Sender: TObject);
var
  SL: TStringList;
begin
  InitMemoLines;
  SL := TStringList.Create;
  try
    Memo1.Lines.BeginUpdate;
    try
      SL.Assign(Memo1.Lines);
      SL.Sort;
      Memo1.Lines.Assign(SL);
    finally
      Memo1.Lines.EndUpdate;
    end;
  finally
    SL.Free;
  end;
end;

// Sorts by extracting the text after the tab character on the lines
// being compared, converting to numbers, and comparing the numbers.
// Called by using SL.CustomSort in the btnScoreSortClick event
// below.
//
// NOTE: Will obviously fail if the lines don't contain a tab, or
// if the content after the tab can't be converted to a numeric.
// Neither of those cases is handled here for brevity. 
function NumberedListSort(List: TStringList; Index1, Index2: Integer): Integer;
var
  Line1, Line2: string;
  Num1, Num2: Integer;
begin
  Line1 := List[Index1];
  Line2 := List[Index2];
  Num1 := StrToInt(Copy(Line1, Pos(#9, Line1) + 1, 255));
  Num2 := StrToInt(Copy(Line2, Pos(#9, Line2) + 1, 255));
  Result := Num1 - Num2;
end;

// Calls NumberedListSort to sort by the numbers on the right end 
// of each line in the memo
procedure TForm1.btnScoreSortClick(Sender: TObject);
var
  SL: TStringList;
begin
  InitMemoLines;
  SL := TStringList.Create;
  try
    Memo1.Lines.BeginUpdate;
    try
      SL.Assign(Memo1.Lines);
      SL.CustomSort(NumberedListSort);
      Memo1.Lines.Assign(SL);
    finally
      Memo1.Lines.EndUpdate;
    end;
  finally
    SL.Free;
  end;
end;

// Calls InitMemoLines to set the starting content of the memo
procedure TForm1.FormCreate(Sender: TObject);
begin
  InitMemoLines;
end;

// Generates content of memo
procedure TForm1.InitMemoLines;
var
  i: Integer;
begin
  Memo1.Lines.Clear;
  for i := 1 to 10 do
    Memo1.Lines.Append(Format('Line ' + Chr(90 - i) + #9 + ' %d', [i]));
end;