Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting Delphi-字符串网格行的降序排序,按1列排序_Sorting_Delphi_Delphi Xe2 - Fatal编程技术网

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

Sorting Delphi-字符串网格行的降序排序,按1列排序,sorting,delphi,delphi-xe2,Sorting,Delphi,Delphi Xe2,我在排序时遇到了一些麻烦,我设法将字符串网格中的行从最小到最大排序,但现在我不知道如何按降序排序。我尝试过使用另一种类型的代码,我只更改了代码中的第二个最后一个循环,以查看是否可以从TStringList的底部读取,但它不起作用,只从列表中提取一行并将其复制到其余行中。是否有一种方法可以在排序后反向读取TStringList 我用于另一种排序的代码,并尝试实现这种排序(只更改了最后的第二个循环): procedure TfrmPuntehou.SortLTSGrid(var grid: TStr

我在排序时遇到了一些麻烦,我设法将字符串网格中的行从最小到最大排序,但现在我不知道如何按降序排序。我尝试过使用另一种类型的代码,我只更改了代码中的第二个最后一个循环,以查看是否可以从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

用于使用特定的比较方法对列表进行排序

给出了比较器的规格

例如:

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;
函数Compare1(//普通alphanum排序
列表:TStringList;
Index1:整数;
Index2:整数):整数;
开始
如果List[Index1]=List[Index2],则
结果:=0
否则,如果List[Index1]
您将如何在使用比较器的同时使用CustomSort过程?(文档中的2页让我对如何实际使用和实现CustomSort过程感到困惑)@PrimeBeat我添加了一个带有两种自定义排序方法的示例代码。