Delphi - 字符串网格行的降序排序,按 1 列排序

问题描述

我在排序时遇到了一些障碍,我设法将字符串网格的行从最小到最大排序,但现在我不确定如何按降序对其进行排序。我试过使用我在其他类型中使用的代码,我只更改了代码中的第二个最后一个循环,以查看是否可以从 TStringList 的底部读取,但它没有用,只需要从 TStringList 中读取一行list 并将其复制到其余的行中。有没有办法在排序后反向读取 TStringList ?

我用于另一种排序的代码,并尝试为这种排序实现(只更改了最后一个循环):

procedure Tfrmpuntehou.sortLTSGrid(var grid: TStringGrid; columntotal: Integer);
const
separator = ',';
var
iCount,i,j,k,iPos:integer;
TheList:TStringList;
sstring,stempString:string;
  m: Integer;
  o: Integer;
begin
  //procedure to sort from large to small values

  //get row amount
  iCount:=grid.RowCount-1;

  //create list
  TheList:=TStringList.Create;
  TheList.sorted:=False;

  //start of try..finally block
    try
    begin

      //fill the list
      for i := 1 to (iCount - 1) do
      begin
        TheList.Add(grid.Rows[i].Strings[columntotal]+separator+grid.Rows[i].Text);
      end;

      //sort the list
      TheList.sort;

      for k := 1 to TheList.Count do
      begin
      //take the line of the list and put it in a string var
      sstring:= TheList.Strings[(k-1)];
      //get separator pos in that string
      iPos:=AnsiPos(separator,sstring);
      stempString:='';
      //remove separator and the column text at the front of the string
      stempString:=copy(sstring,(iPos+1),Length(sstring));
      TheList.Strings[(k-1)]:= '';
      TheList.Strings[(k-1)]:= stempString;
      end;

      //fill the grid
      for j:= (iCount - 1) downto 1 do
      begin
        for o := 1 to (iCount - 1) do
          begin
            grid.Rows[j].Text := TheList.Strings[(o-1)] ;
          end;
      end;

      //fill the row numbers
      for m := 1 to iCount do
      begin
      grid.Cells[0,m]:= IntToStr(m);
      end;

    end;
    finally
    TheList.Free;
    end;
  //end of try..finally block
end;

预先感谢您的帮助!
亲切的问候
PrimeBeat

解决方法

使用 TStringList.CustomSort 使用特定的比较方法对列表进行排序。

比较器的规范为 here

示例:

function Compare1(           // Normal alphanum sort
    List   : TStringList;
    Index1 : Integer;
    Index2 : Integer) : Integer;
begin
    if List[Index1] = List[Index2] then
        Result := 0
    else if List[Index1] < List[Index2] then
        Result := -1
    else
        Result := 1;
end;

function Compare2(           // Reverse alphanum sort
    List   : TStringList;
    Index1 : Integer;
    Index2 : Integer) : Integer;
begin
    if List[Index1] = List[Index2] then
        Result := 0
    else if List[Index1] < List[Index2] then
        Result := 1
    else
        Result := -1;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
    SList : TStringList;
    S     : String;
begin
    SList := TStringList.Create;
    try
        SList.Add('Pierre');
        SList.Add('Albert');
        SList.Add('Paul');
        SList.Add('Jean');
        SList.Add('Simon');


        Memo1.Lines.Add('=== Compare1 ===');
        SList.CustomSort(Compare1);
        for S in SList do
            Memo1.Lines.Add(S);

        Memo1.Lines.Add('=== Compare2 ===');
        SList.CustomSort(Compare2);
        for S in SList do
            Memo1.Lines.Add(S);
    finally
        SList.Free;
    end;

end;