2013-10-25 120 views
2

排序备忘录我必须做出一个高分备忘录为我在Delphi 6 学校是有办法的数字或字母的排序MemoLines?在Delphi

我使用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排序每场比赛的最高分?

+0

我认为你应该看看创建一个数据对象和排序数据对象。然后你应该将数据对象(已经排序)打印到字符串列表中。你陷入了只使用内置数据类型(特别是字符串)的“字符串类型”坏习惯,你应该考虑创建自己的记录或类。 –

回答

2

下面是一些示例代码,将让你实验排序。它在每行上使用文本值和数字,用制表符分隔(#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; 
+0

Thx为答案 –

3

我认为最简单的办法是沿着这些路线的东西:

  1. 转移从备忘录到TStringList实例的内容。
  2. 呼叫CustomSortTStringList情况下,通过适当的比较排序功能。
  3. 将内容传回备忘录。

步骤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是根据您的规则两个字符串,其比较功能。该函数的返回值遵循比较函数的惯例。负数表示小于,正数表示大于零表示相等。

当然,你可以,如果你喜欢写逻辑直接内嵌到MySortCompare

+0

非常感谢你的伟大答案,但我不明白我必须在项目中放置第1-3步的代码。 –

+0

嗯,这是你的代码不是我的。由于只有你能看到你的代码,你几乎不能指望我们能够准确地告诉你要写什么和在哪写。您问过如何根据一些自定义排序对备忘录进行排序。在代码中您需要执行此操作的位置,在步骤1-3中进行拼接。你应该关注的是理解。 –

+1

我期待从你那里得到一个[slicker way](http://stackoverflow.com/questions/11122197/why-does-memo-lines-use-tstrings-instead-of-tstringlist/11122492#comment14576040_11122492)...; - )Btw +1 – NGLN