How do I sort a generic list using a custom comparer?

后端 未结 4 1703
谎友^
谎友^ 2020-11-30 00:08

I\'m kinda a Delphi-newbie and I don\'t get how the Sort method of a TList of Records is called in order to sort the records by ascending integer value. I have a record like

4条回答
  •  鱼传尺愫
    2020-11-30 00:22

    I want to share my solution (based on the input I have gathered here).

    It's a standard setup. A filedata class that holds data of a single file in a generic TObjectList. The list has the two private attributes fCurrentSortedColumn and fCurrentSortAscending to control the sort order. The AsString-method is the path and filename combined.

    function TFileList.SortByColumn(aColumn: TSortByColums): boolean;
    var
      Comparison: TComparison;
    begin
      result := false;
      Comparison := nil;
    
      case aColumn of
        sbcUnsorted   : ;
        sbcPathAndName: begin
                          Comparison := function(const Left, Right: TFileData): integer
                                        begin
                                          Result := TComparer.Default.Compare(Left.AsString,Right.AsString);
                                        end;
                        end;
        sbcSize       : begin
                          Comparison := function(const Left, Right: TFileData): integer
                                        begin
                                          Result := TComparer.Default.Compare(Left.Size,Right.Size);
                                          if Result = 0 then
                                            Result := TComparer.Default.Compare(Left.AsString,Right.AsString);
                                        end;
                        end;
        sbcDate       : begin
                          Comparison := function(const Left, Right: TFileData): integer
                                        begin
                                          Result := TComparer.Default.Compare(Left.Date,Right.Date);
                                          if Result = 0 then
                                            Result := TComparer.Default.Compare(Left.AsString,Right.AsString);
                                        end;
                        end;
        sbcState      : begin
                          Comparison := function(const Left, Right: TFileData): integer
                                        begin
                                          Result := TComparer.Default.Compare(Left.FileDataResult,Right.FileDataResult);
                                          if Result = 0 then
                                            Result := TComparer.Default.Compare(Left.AsString,Right.AsString);
                                        end;
                         end;
      end;
    
      if assigned(Comparison) then
      begin
        Sort(TComparer.Construct(Comparison));
    
        // Control the sort order
        if fCurrentSortedColumn = aColumn then
          fCurrentSortAscending := not fCurrentSortAscending
        else begin
          fCurrentSortedColumn := aColumn;
          fCurrentSortAscending := true;
        end;
    
        if not fCurrentSortAscending then
          Reverse;
    
        result := true;
      end;
    end;
    

提交回复
热议问题