Вопрос Сортировка списка (TStringList)

Andreo Fadio

Старожил
Как отсортировать лист с файлами в название у которых есть цифры, чтобы были по порядку?
к примеру (как получается):
.res1
.res20
.res21
.res2
.sid1
.sid10
.sid11
.sid2
Как чтобы стало:
.res1
.res2
.res20
.res21
.sid1
.sid2
.sid10
.sid11
 

HandyMan

Новичок
Может это подойдет...
Sort an array of numbers in Inno Setup - Stack Overflow
Код:
//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;
Код:
procedure QuickSort(var Strings: array of String; Start, Stop: Integer);
var
  Left: Integer;
  Right: Integer;
  Mid: Integer;
  Pivot: string;
  Temp: string;
begin
  Left  := Start;
  Right := Stop;
  Mid   := (Start + Stop) div 2;

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

  if Start < Right then QuickSort(Strings, Start, Right); // Uses
  if Left < Stop then QuickSort(Strings, Left, Stop);     // Recursion
end;
 
Последнее редактирование:
Сверху