IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Codes sources à télécharger Delphi Discussion :

Tri rapide d'une grille (TStringGrid)


Sujet :

Codes sources à télécharger Delphi

  1. #1
    Membre chevronné
    Avatar de Pierre Castelain
    Inscrit en
    Avril 2002
    Messages
    523
    Détails du profil
    Informations forums :
    Inscription : Avril 2002
    Messages : 523
    Points : 1 943
    Points
    1 943
    Par défaut Tri rapide d'une grille (TStringGrid)
    Bonjour,

    Je vous propose un nouvel élément à utiliser : Tri rapide d'une grille (TStringGrid)

    Exemple de tri par colonne d'un composant TStringGrid à l'aide d'un algorithme de tri rapide (quick sort)

    Qu'en pensez-vous ?

  2. #2
    Membre expert
    Avatar de Charly910
    Homme Profil pro
    Ingénieur TP
    Inscrit en
    Décembre 2006
    Messages
    2 344
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur TP
    Secteur : Bâtiment Travaux Publics

    Informations forums :
    Inscription : Décembre 2006
    Messages : 2 344
    Points : 3 122
    Points
    3 122
    Par défaut
    Bonjour,

    pour trier une colonne, j'utilise une unité d'Olivier Dahan qui fonctionne bien (peut être pas aussi vite qu'avec QuickSort ?) et qui peut trier des valeurs alphabétiques, numériques, des dates ou des heures. Le voici

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    unit SortGrid;
     
    { © Olivier Dahan - odahan@cybercable.fr }
     
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ComCtrls, Grids, StdCtrls;
     
    type
     TodColType = (odct_Date,odct_time,odct_DateTime,odct_numeric,odct_CaseString,odct_NoCaseString);
     
    Procedure ODSortGrid(grid:TStringGrid;ColToSort:Integer;TypeCol:TodColType;FromRow,ToRow:integer;Ascending:Boolean);
     
    implementation
     
    Procedure ODSortGrid(grid:TStringGrid;ColToSort:Integer;TypeCol:TodColType;FromRow,ToRow:integer;Ascending:Boolean);
    var Ts:TStringList; i:integer; sg:tstringgrid;
     Function ToDate(const s:string):string;
     var y,m,d : word;
     begin
      DecodeDate(StrToDate(s),y,m,d);
      result := FormatFloat('0000',y)+FormatFloat('00',m)+FormatFloat('00',d);
     end;
     Function ToTime(const s:string):string;
     var h,m,sx,ms:word;
     begin
      DecodeTime(StrToTime(s),h,m,sx,ms);
      result := FormatFloat('00',h)+FormatFloat('00',m)+FormatFloat('00',sx)+FormatFloat('00',ms);
     end;
     Function ToDateTime(const s:string):string;
     var p:integer;
     begin
      p:=pos(' ',s);
      if p>0 then
       begin
        Result := ToDate(copy(s,1,p-1))+ToTime(copy(s,p+1,length(s)));
       end else result := '0000000000000000';
     end;
     Function ToNumeric(const s:string):string;
     var p:integer; sx:string;
     const z30 = '000000000000000000000000000000';
     function pad(z:integer):string;
     begin
      if z in [1..30] then result := copy(z30,1,z) else result :='';
     end;
     begin
      sx:='';
      for p:=1 to length(s) do
       if s[p] in (['0'..'9','-',',','.']-[ThousandSeparator]) then sx:=sx+s[p];
      p:=pos(DecimalSeparator,sx);
      if p>0 then
      result := pad(25-length(copy(sx,1,p-1)))+copy(sx,1,p-1)+'.'+copy(sx,p+1,length(sx))+pad(25-length(copy(sx,p+1,length(sx))))
      else result := pad(25-length(sx))+sx;
     end;
    begin
     ts:=tstringlist.Create;
     try
      For i:=FromRow to ToRow do
       begin
        Case TypeCol of
         odct_Date : ts.AddObject(ToDate(grid.cells[ColToSort,i]),tobject(i));
         odct_Time : ts.AddObject(ToTime(grid.cells[ColToSort,i]),tobject(i));
         odct_DateTime : ts.AddObject(ToDateTime(grid.cells[ColToSort,i]),tobject(i));
         odct_Numeric : ts.AddObject(ToNumeric(grid.cells[ColToSort,i]),tobject(i));
         odct_CaseString : ts.AddObject(grid.cells[ColToSort,i],tobject(i));
         odct_NoCaseString : ts.AddObject(AnsiUpperCase(grid.cells[ColToSort,i]),tobject(i));
        end;
       end;
      ts.sorted := true;
      sg := TStringGrid.Create(application);
      try
       sg.ColCount := grid.colcount;
       sg.rowcount := ToRow-FromRow+1;
       sg.FixedCols := 0;
       sg.FixedRows := 0;
       if Ascending then
        For i:=0 to ts.count-1 do
         sg.Rows[i] := grid.Rows[integer(ts.objects[i])]
        else
        For i:=ts.count-1 downto 0 do
         sg.Rows[(ts.count-1)-i] := grid.Rows[integer(ts.objects[i])];
       For i:=FromRow to ToRow do
        Grid.Rows[i] := sg.Rows[i-FromRow];
      finally
       sg.free;
      end;
     finally
      ts.free;
     end;
    end;
     
    end.
    Tu pourrais peut être faire un mix des 2, car ton exemple est plus complet ?

    A+
    Charly

Discussions similaires

  1. Réponses: 13
    Dernier message: 25/04/2012, 18h04
  2. Tri rapide d'une CListCtrl
    Par vanitom dans le forum MFC
    Réponses: 29
    Dernier message: 03/10/2008, 08h34
  3. Réponses: 16
    Dernier message: 10/11/2005, 22h51
  4. Redessiner une grille d'un TStringGrid
    Par delphi5user dans le forum Composants VCL
    Réponses: 9
    Dernier message: 12/10/2004, 15h04
  5. jaimerais savoir commen creer une grille.......
    Par zephyr dans le forum Flash
    Réponses: 5
    Dernier message: 29/04/2003, 12h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo