2013-10-25 66 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