0

以下のコードを使用して、ファイル内の行をソートしています。以下のように入力行を与えました:

6 5 1 12 10

しかし、以下のように出ています:

10 12 1 5 6

I need out as

1 5 6 10 12

Inno Setup で数値行をソートする方法はありますか。

procedure SortList(const FileName: string);
var
  I: Integer;
  Files: TStringList;
begin
  Files := TStringList.Create;
  try
    Files.LoadFromFile(FileName);
    for I := Files.Count - 1 downto 0 do
    begin
      Files.sort;
    end;
    Files.SaveToFile(FileName);
  finally
    Files.Free;
  end;
end;

前もって感謝します。

4

1 に答える 1

4

次の Quicksort proc でジョブを実行する必要があります。

//Start is the index of the first item on the list - usually 0
//Stop is the index of the last item of the list e.g. Count - 1
procedure QuickSort(var List: TStringList; Start, Stop: Integer);
var
  Left: Integer;
  Right: Integer;
  Mid: Integer;
  Pivot: integer;
  Temp: integer;
begin
  Left  := Start;
  Right := Stop;
  Mid   := (Start + Stop) div 2;

  Pivot := StrToInt(List[mid]);
  repeat
    while StrToInt(List[Left]) < Pivot do Inc(Left);
    while Pivot < StrToInt(List[Right]) do Dec(Right);
    if Left <= Right then
    begin
      Temp           := StrToInt(List[Left]);
      List[Left]  := List[Right]; // Swops the two Strings
      List[Right] := IntToStr(Temp);
      Inc(Left);
      Dec(Right);
    end;
  until Left > Right;

  if Start < Right then QuickSort(List, Start, Right); // Uses
  if Left < Stop then QuickSort(List, Left, Stop);     // Recursion
end;

呼び出す代わりに:

Files.sort;

以下を使用します。

 QuickSort(Files, 0, Files.Count - 1);

1 つの注意点は、他の場合のエラー処理を追加していないため、ファイルの内容は常に有効な整数でなければならないということです。

私が使用したクイックソート機能は、Torry's Delpi にあるものを修正したものです: http://www.swissdelphicenter.ch/torry/showcode.php?id=1916

于 2013-11-11T01:00:20.603 に答える