【问题标题】:Delphi - descending sort of string grid rows, sorting by 1 columnDelphi - 字符串网格行的降序排序,按 1 列排序
【发布时间】:2021-01-17 13:31:34
【问题描述】:

我的排序有点困难,我设法将字符串网格的行从小到大排序,但现在我不知道如何按降序排序。我已经尝试使用我使用的另一种代码,我只更改了代码中的第二个最后一个循环,以查看我是否可以从 TStringList 的底部读取,但它没有工作,只需要一行列出并将其复制到其余行中。是否有办法在排序后反向读取 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

【问题讨论】:

    标签: sorting delphi delphi-xe2


    【解决方案1】:

    使用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;
    

    【讨论】:

    • 您将如何将 CustomSort 过程与比较器一起使用? (文档中的 2 页让我对如何实际使用和实施 CustomSort 过程感到困惑)
    • @PrimeBeat 我添加了一个带有两种自定义排序方法的示例代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2012-04-28
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多